这是我的代码,我们称之为“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 编程的新手,所以如果有人能解决这个问题或至少我出错的地方,我将不胜感激