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)))
Run Code Online (Sandbox Code Playgroud)
这工作正常,我已经测试过,当目录不存在时它不会窒息而死,但我正在读一本Python书,我应该在打开文件时使用"with".有没有一种首选方式来做我正在做的事情?