Printing the error encountered with Python

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."

Python by Example
Python by Example
1x1 px

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.

Or for Python 2.6 or later, I could use the following for the except statement to see the same output:

except Exception as e:
   print e     
   print "Error! Unable to make a backup copy of the input file."
   sys.exit(1)

Once I saw the "permission denied" error message for the attempt to create the backup file I was able to discern the cause of the problem and fix it. There was already a prior backup file, /Users/jasmith1/Documents/www/SGRS/data/SGRS_Count.csv.bak that I had created under my account on the system and Apache was unable to overwrite it.

$ ls -l /Users/jasmith1/Documents/www/SGRS/data/
total 1104
-rw-r--r--@ 1 jasmith1  ABC\Domain Users     766 Jul  3 22:24 SGRS_Count.csv
-rw-r--r--  1 jasmith1  ABC\Domain Users     728 Jul  3 22:24 SGRS_Count.csv.bak

I could see that only my account had write access to the file, so I granted write permission for the group and then changed the group ownership of the file to the group used by Apache on the system, which was _www (Apache is running on an OS X system), so that Apache could write to the file, creating a new version of the backup file. When I then reran the Python script from the webpage, I no longer received the error message and I found a new backup file had been created.

Mastering Python
Mastering Python
1x1 px

$ chmod g+w ../SGRS/data/SGRS_Count.csv.bak
$ sudo chgrp _www ../SGRS/data/SGRS_Count.csv.bak
Password:
$ ls -l /Users/jasmith1/Documents/www/SGRS/data/
total 1104
-rw-r--r--@ 1 jasmith1  ABC\Domain Users     766 Jul  3 22:24 SGRS_Count.csv
-rw-rw-r--  1 jasmith1  _www                 766 Jul  7 21:36 SGRS_Count.csv.bak

References:

  1. How to print an error in Python?
    Asked: September 27, 2009
    Stack Overflow

Related articles:

  1. Running an Apache web server under OS X El Capitan
  2. PHP for Apache on OS X El Capitan
  3. Using Python scripts with Apache on OS X El Capitan