nuT707 nuT707 - 1 year ago 122
Python Question

How to open file using argparse?

I want to open file for reading using argparse.
In cmd it must look like: /filepath

That's my try:

parser = argparse.ArgumentParser()
parser.add_argument('file', type = file)
args = parser.parse_args()

wim wim
Answer Source

The type of the argument should be string (which is default anyway). So make it like this:

parser = argparse.ArgumentParser()
args = parser.parse_args()
with open(args.filename) as file:
  # do stuff here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download