Reputation: 229
This is a part of python script xyz.py
.
#!/usr/bin/env python
from openpyxl import Workbook
import os
wb = Workbook()
path = "/home/Final_analysis/"
#print(os.listdir())
lis = os.listdir(path)
I wanted to use this script with different input files which are stored in different directories. For this script to run on multiple files I would have to change this part of the script every time.
path= "/home/Final_analysis/"
Can I parse the path as argument after python script for example
xyz.py path_to_my_file
in the command so that each time I don't have to change the script?
I tried running script as above after writing sys.argv[1]
in path
#!/usr/bin/env python
from openpyxl import Workbook
import os
wb = Workbook()
path = "sys.argv[1]"
#print(os.listdir())
lis = os.listdir(path)
and ran command xyz.py /home/final_analysis
but it still doesn't detect the path. I am getting following error
Traceback (most recent call last):
File "combine_excel.py", line 8, in <module>
lis = os.listdir(path)
OSError: [Errno 2] No such file or directory: 'sys.argv[1]'
I am using in python 2.7.6.
Upvotes: 0
Views: 391
Reputation: 1175
In addition to what people have commented on you post,
you can also use the "{}".format()
syntax.
A really clean way to insert string into an already formed string.
Upvotes: 0
Reputation: 12201
Don't pass sys.argv[1]
as a string, use it directly: path = sys.argv[1]
#!/usr/bin/env python
from openpyxl import Workbook
import os
import sys
wb = Workbook()
path = sys.argv[1]
#print(os.listdir())
lis = os.listdir(path)
(per zwer's comment to the question.)
Upvotes: 1