我正在尝试将多个复制命令的成功或失败记录到日志文件中。我正在使用 shutil.copy()
- 例如
str_list.append(getbitmapsfrom)
game.bigbitmap = "i doubt this is there.bmp"
str_list.append(game.bigbitmap)
source = '\\'.join(str_list)
shutil.copy(source, newbigbmpname)
我强制我的脚本中的一个复制命令失败,它产生了错误:
[Errno 2] 没有这样的文件或目录:'X:\PJ_public\PJ_Services\BSkyB-PlayJam\Content\P_NewPortal2009\1.0.0\pframes\i doubt this is there.bmp'
这很好,但是我可以捕获 "Errno 2 No such file or directory"
并将其写入日志文件吗? shutil.copy()
是否返回整数值? - 我没有在 Python 文档中看到这一点。
我想我也希望能够捕获返回值,这样脚本就不会在复制失败时崩溃——我正试图让它继续运行,不管有没有错误。
谢谢。
最佳答案
您需要查看 exceptions section的 Python tutorial .如果 shutil.copy() 找不到参数之一,将引发 IOError 异常。您可以从异常实例中获取消息。
try:
shutil.copy(src, dest)
except IOError, e:
print "Unable to copy file. %s" % e
关于python - 如何在 Python(在 DOS 上)中捕获 shutil.copy() 的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1662261/