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()
In order to have the file closed gracefully, you can combine argparse.FileType with the "with" statement
# .... parser.add_argument('file', type=argparse.FileType('r')) args = parser.parse_args() with args.file as file: print file.read()
--- update ---
Oh, @Wernight already said that in comments