문제

if we take a look at a file copy function, we can see there are several exceptions to handle. A good example is here: http://msdn.microsoft.com/en-us/library/9706cfs5.aspx

my question is if i use python shutil copy2, what should I pay attention to cope with various exceptions (source file not found, access not authorized, etc.)?

e.g.

def copy_file (self):   
    if not os.path.isdir(dest_path):
        os.makedirs(dest_path)
    shutil.copy2(src_path, dest_path)

what should i do to the above function?

도움이 되었습니까?

해결책

You may just need handle the IOError exception that may be caused due to any permissions or Invalid destination name issue.

try:
    shutil.copy(src,dst)
except IOError as e:
    print e

The other exceptions mentioned in the MSDN article seems to fall under the same IOError in python. The FileNotFound and DirectoryNotFound are not really applicable as shutil.copy will create the destination if it not already exists. Also, I find that happening of OSError are also remote this in case.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top