1

I'm a beginner python programmer using it for simple admin tasks.

I have written a small log moving code (as I have a system that generates alot of log files that get moved from the server to storage).

I am getting a permissions error on a file on the remote path has permission denied when trying to copy so have wrapped this in a try: statement to try and by pass the error but the script is still ending... line 32 is shutil.copy2 (location,copylocation).

Base path is a local directory and log dir is a UNC path (which is appended with the folder set). Folders are mirrored across both file systems earlier in the script. (so should always exist).

Other try's such as around the os.remove work OK.

If anyone could point me in the general direction would be appreciated.

Traceback (most recent call last):
  File "C:\Program Files (x86)\Log Mover\log_mover.py", line 32,
  shutil.copy2 (location,copylocation)
  File "C:\Python27\lib\shutil.py", line 127, in copy2
  copyfile(src, dst)
  File "C:\Python27\lib\shutil.py", line 82, in copyfile
    with open(dst, 'wb') as fdst:
  IOError: [Errno 13] Permission denied: '\\\\XXXX\\d$\\logs\\
  logs/XXX\\XXX - 2012-07-03 21-49.XXXX'
  ☀could not delete:C:/logs/XX\XXX\XXX 2012-07-04 08-00-42.txt
  Traceback (most recent call last):


for root, dirs, files in os.walk(basepath):
   for filenames in files:
        location = os.path.join(root, filenames)
        try:
            datemod = time.strftime("%Y%m%d%H",time.localtime(os.path.getmtime(location)))
        except OSError:
            print "count not get mod time:"+location
        if datemod != currtime:
            drive,path = os.path.splitdrive(location)
            copylocation = str(logdir)+str(path)
            try:
                shutil.copy2 (location,copylocation)
            except OSError:
                print "could not copy"
            pass

            if os.path.getsize(location) == os.path.getsize(copylocation):
                try:
                    os.remove(location)
                    print "Deleted"+location
                except OSError:
                    print "could not delete:"+location
                pass
            else:
                print "sizes dont match"

Thanks

Kenny

4

2 回答 2

3

You're trying to catch OSError, while you get IOError.

Add this:

except (OSError, IOError):
    #some code
于 2012-07-04T17:00:04.280 回答
2

shutil.copy2 throws an IOError, but you're catching OSErrors, so therefore it just bubbles past, uncaught.

于 2012-07-04T16:59:46.337 回答