【发布时间】:2015-06-04 16:41:30
【问题描述】:
我编写了一个脚本来扫描文本文件的目录,如果找到它们,它会创建一个系统调用来在 txt 文件上运行脚本。我仍在处理一些导致我的一些系统调用失败的小错误。但是,我希望这不会杀死我的脚本。我只想知道错误然后继续我的生活。似乎任何成功的调用都返回 0,而任何导致错误的调用都返回 n。我试图将结果与 0 进行比较,但它永远不会那么远。关于我如何做到这一点的任何建议?
import sys, getopt, os
def main(argv):
def scan_dir(path):
temp_path = path
print temp_path
for file in os.listdir(path):
temp_path += file
if file.endswith(".txt"):
result = os.system("python callscript.py -i %s" % path)
if result != 0
print "Error!"
temp_path = path
def usage():
print "usage: dostuff.py [hi:]\n \
\t -h\t print usage\n \
\t -i\t directory path\n"
sys.exit(2)
if(len(argv) == 0):
usage()
path = ''
try:
opts, args = getopt.getopt(argv,"hi:",["path="])
except getopt.GetoptError:
usage()
for opt, arg in opts:
if opt == '-h':
usage()
elif opt in ("-i", "--ipath"):
path = arg
if path.endswith('/') == False:
path += '/'
scan_dir(path)
if __name__ == "__main__":
main(sys.argv[1:])
【问题讨论】:
-
你可以试试看,除了这个
-
如果您的循环在 Python 中并且脚本在 Python 中,为什么首先使用
os?