Python - 如果action为true,则打印文本

Dan*_*Dan 0 python printing if-statement

如果成功复制文件,我正在尝试让Python打印一个句子.在复制文件时,它会忽略打印.为什么是这样?这是我的代码的类似示例:

from shutil import copyfile

if copyfile('/Library/demo.xls','/Jobs/newdemo.xls'):
  print "the file has copied"
Run Code Online (Sandbox Code Playgroud)

作为参考,我使用的是Python v2.7.1

Gar*_*Jax 9

copyfile不返回任何内容,但如果发生错误则抛出异常.使用以下习语而不是if检查:

import shutil

try:
    shutil.copyfile('/Library/demo.xls','/Jobs/newdemo.xls')
except (Error, IOError):
    # Handle error
    pass
else:
    # Handle success
    print "the file has copied"
Run Code Online (Sandbox Code Playgroud)

链接到shutil.copyfile文档.