["python","argparse"]
How to open file using argparse?

I want to open file for reading using argparse. In cmd it must look like: my_program.py /filepath That's my try: parser = argparse.ArgumentParser() parser.add_argument('file', type = file) args = parser.parse_args() This gives me [edit: added later from comment by OP]: parser.add_argument('file', type = file) NameError: name 'file' is not defined
The type of the argument should be string (which is default anyway). So make it like this: parser = argparse.ArgumentParser() parser.add_argument('filename') args = parser.parse_args() with open(args.filename) as file: # do stuff here