title: Try statement not catching copy error python
body: <p>I'm a beginner python programmer using it for simple admin tasks.</p>
  
  <p>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).</p>
  
  <p>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).</p>
  
  <p>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).</p>
  
  <p>Other try's such as around the os.remove work OK.</p>
  
  <p>If anyone could point me in the general direction would be appreciated.</p>
  
  <pre><code>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"
  </code></pre>
  
  <p>Thanks</p>
  
  <p>Kenny</p>