我的python代码在bash脚本中调用,而python脚本也需要bash循环的输入参数。但我不确定这样做的最好方法。这是我的尝试:
bash代码:
cat filelList.txt | while read i;#file1List.txt stores file names
do
python script.py $i > $i.txt
done
我不确定的蟒蛇:
file = sys.argv[0] # not sure about this
in = open(file)
for line in iter(in): # not sure about this
do something.......
有谁知道写这3条python线的最佳方法?谢谢
答案 0 :(得分:1)
为什么不在python中做所有事情?假设您有一个文件夹,您必须处理每个文件:
import sys
import os
assert os.path.isdir(sys.argv[1])
listing = os.listdir(sys.argv[1])
for filename in listing:
with open(sys.argv[1]+"/"+filename,'r') as f:
for line in f:
#do something
pass
或者,如果您有一个包含要处理的其他文件列表的文件:
import sys
import os
assert os.path.isfile(sys.argv[1])
with open(sys.argv[1]) as filelist:
filenames = [line.strip() for line in filelist]
for filename in filenames:
if not os.path.isfile(filename):
print >> sys.stderr , "{0} is not a valid file".format(filename)
break
with open(filename,'r') as f:
for line in f:
#do something
pass