这是我的代码运行,让我们称它为“example.py”程序空闲而不是通过命令行
import traceback
import sys
import os
import linecache
try:
data_files = [x[2] for x in os.walk(os.path.dirname(sys.argv[0]))]
fileNameList = data_files[0] # gets the list of files in the directory where example.py is present
execfile("sample.py")
except:
这个节目完美的作品,当我运行它通过IDLE,但给了我下面的错误时我通过命令行运行它
Traceback (most recent call last):
File "example.py", line 15, in <module>
fileNameList = data_files[0]
IndexError: list index out of range
我只能假设它不能获得通过命令行的文件夹中的文件列表,我是新来的Python编程,所以我会非常感激,如果有人能解决这个问题或至少我出错的地方
你使用什么命令从命令行调用它?你在做'python example.py文件夹'吗? –
另外,为什么你有'except'语句,为什么它的最后一行(除了块以外呢?) –
检查sys.argv的值[0] – joaquin