python - 如何在 Python(在 DOS 上)中捕获 shutil.copy() 的返回值?

标签 python exception logging

我正在尝试将多个复制命令的成功或失败记录到日志文件中。我正在使用 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 sectionPython 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/

相关文章:

logging - 如何在Seq中查询json文件?

logging - 如何更改 cqlsh (cqlsh_history) 日志文件位置?

python - django-多语言和模板端语言之间的切换

python - 我的流程发生了什么?

java - 使用 testng 框架运行 selenium 测试时出现空异常错误

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

python - 如何在 Panda 的 from_dict (Python) 中使用 dtype 参数

python - 如何检查输出是否包含特定单词?

c# - 从 MVC4 中的 Authorize 属性捕获异常

c++ - C++ 中的异步线程安全日志记录