我有这样的内容纺织:为什么sys.argv()给我一个索引超出限制?
honda motor co of japan doesn't expect output at its car manufacturing plant in thailand
当我跑厕所-l TextFile.txt的,我收到0
的问题是我运行,需要算一个python脚本此文本文件中的行数并相应地运行。我尝试了两种计算行数的方法,但他们都一直给我0,我的代码拒绝运行。
Python代码:
#Way 1
with open(sys.argv[1]) as myfile:
row=sum(1 for line in myfile)
print(row)
#Way 2
row = run("cat %s | wc -l" % sys.argv[1]).split()[0]
我收到一个错误,指出:with open(sys.argv[1]) as myfile IndexError: list index out of range
我从PHP调用接收这个文件:
exec('python testthis.py $file 2>&1', $output);
我怀疑argv.sys [1 ]给我一个错误。
如果'sys.argv [1]'抛出一个'IndexError',那么它没有第二个元素。你有没有试过看它包含什么? – jonrsharpe
听起来'$ file'正在扩展为一个空字符串,所以你没有参数调用你的Python程序。 –