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()
Take a look at the documentation: https://docs.python.org/3/library/argparse.html#type
import argparse parser = argparse.ArgumentParser() parser.add_argument('file', type=argparse.FileType('r')) args = parser.parse_args() print(args.file.readlines())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With