[英]calling python script that requires an input from within bash for loop
My python code is called within a bash script, and also the python script requires an input argument from bash loop. 我的python代码在bash脚本中调用,并且python脚本也需要bash循环中的输入参数。 But I wasn't sure the best way to do this. 但是我不确定做到这一点的最佳方法。 Here's my attempt: 这是我的尝试:
bash code: bash代码:
cat filelList.txt | while read i;#file1List.txt stores file names
do
python script.py $i > $i.txt
done
python bit that i'm not sure of: 我不确定的python位:
file = sys.argv[0] # not sure about this
in = open(file)
for line in iter(in): # not sure about this
do something.......
Does anyone know the best way to write these 3 python lines? 有谁知道编写这3条python行的最佳方法? Thank you 谢谢
Why not do everything in python? 为什么不使用python做所有事情? Assuming you have a folder where you have to process each file: 假设您有一个必须处理每个文件的文件夹:
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
Alternatively if you have a file containing a list of other files to process: 或者,如果您的文件包含要处理的其他文件的列表:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.