Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() 
like image 243
nuT707 Avatar asked Sep 18 '13 02:09

nuT707


1 Answers

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()) 
like image 69
ilent2 Avatar answered Sep 21 '22 23:09

ilent2