【发布时间】:2023-11-24 09:27:02
【问题描述】:
我正在使用以下 python 代码使用 try except 方法在目录D:\PYTHON FILES 中运行多个 python 文件。我需要打印运行失败的文件名。请参考下面的代码。鉴于此,有人可以帮助我吗?
请注意:
请在 DOS 提示符下运行,不要使用IPython。将所有 python 文件包括一个损坏的文件放在目录D:\PYTHON FILES
import os, glob
dir='D:\PYTHON_FILES'
os.chdir(dir)
files = glob.glob ('*.py');
for eachfile in files:
try:
os.system('python '+eachfile+'\n')
except:
print(eachfile + ' missed out')
finally:
print('======================')
【问题讨论】:
-
您阅读过
os.system上的文档吗?它不会抛出错误,它会返回一个非零退出代码。
标签: python operating-system ipython glob