Pavlos Pavlos - 2 months ago 8x
Python Question

Randomly parsing files in Python from the command line

When I was programming in C I had figured out a way in order to parse command line arguments for my program in any order, but I just would have to specify what the file was.
So my input was something like this:

./exe.out -i <inputfile> -o <outputfile> -a <someotherfile> etc...
And this could get mixed up like so:
./exe.out -o <outputfile> -i <inputfile> -a <someotherfile> etc...

Now I need to do this in python. Meaning, having a method which will handle command line arguments in the beginning of the program and return the file locations to the main program.

python -i <inputfile> -o <outputfile> -a <someotherfile> etc...

Can this be done? I've found a way to do it, but the order has to precise and this will not do for my project.


You are looking for optparse (before Python 2.7) or argparse (Python 2.7 or above).