try:
directoryListing = os.listdir(inputDirectory)
#other code goes here, it iterates through the list of files in the directory
except WindowsError as winErr:
print("Directory error: " + str((winErr)))
这很好用,我已经测试过当目录不存在时它不会阻塞和死亡,但是我正在阅读一本 Python 书籍,我应该在打开文件时使用“with”。有没有首选的方法来做我正在做的事情?