In a Python script I was calling from a webpage residing on an
Apache webserver, I was unable to copy a file
whose location and file name were stored in the variable countfile
to another file whose name and location were stored in the variable
backupfile
with any of the following lines of code:
shutil.copy(countfile, backupfile)
shutil.copy2(countfile, backupfile)
shutil.copyfile(countfile, backupfile)
The backup file should be stored in the same directory as
countfile
using the same name, but with ".bak" appended to the
file name. I wasn't able to identify the cause of the problem when I just used
except
to print my own error message with the code below, since
all I would see in the output was "Error! Unable to make a backup
copy of the input file."
import shutil backupfile = countfile + ".bak" try: # Make a backup copy of the prior file shutil.copy(countfile, backupfile) except e: print "Error! Unable to make a backup copy of the input file." sys.exit(1)
But the explicit Python error message can be obtained by using
except Exception, e
as shown below:
try: shutil.copy(countfile, backupfile) except Exception,e: print str(e) print "Error! Unable to make a backup copy of the input file." sys.exit(1)
When I used that code, instead, I saw the following output for the error, which showed that the problem was with the creation of the backup file:
[Errno 13] Permission denied: '/Users/jasmith1/Documents/www/SGRS/data/SGRS_Count.csv.bak' Error! Unable to make a backup copy of the input file.
[ More Info ]