My script currently uses sys.argv to check for an input file provided to the program.
I am trying to utilise argparse instead but I cant seem to get it to work. I was able to set it up and add an argument, but when I parse an argument, and print that parsed argument, I get a namespace. How can I get a string? Basically, I want to take the argument as a string, and open a file with that name.
Currently, my sys.argv is:
filename = sys.argv[1]
f = open(filename, 'r')
My argparse prints out a Namespace as follows:
arg = parser.parse_args()
print arg
How can I use that to open a file? I want to use argparse since the error handlign for arguments there is a lot easier.
think its preferable (or something!) to use the
withstatement to open the file like this:specifying those keyword args also helps make a pretty -h help text option (which is neat neat)