Reputation: 103
I have a python script func.py that contains argparse and takes a file
parser = argparse.ArgumentParser(description='test')
parser.add_argument('-i', dest='ifile', metavar="FILE", help='input file')
args = parser.parse_args()
This ifile is basically just a name of that file in a directory
filename = 'PATH TO FILE' + args.ifile
If I have a variable that contains the name of the file how can I pass the variable to the argparse so that it would take the string value of the variable? Something like this :
new_file = 'text_file.txt'
func.py -i new_file
filename = 'PATH TO FILE' + 'text_file.txt'
What I am getting now is this:
new_file = "text_file.txt'
python func.py -I new_file
filename = 'PATH TO FILE' + 'new_file'
And then obviously an error message that it cannot find the file.
Upvotes: 1
Views: 410
Reputation: 47169
You should simply be able to do:
$ new_file="text_file.txt"
$ ./func.py -I $new_file
Your args variable in Python
should then contain ifile='text_file.txt'
:
text_file.txt
Upvotes: 2