another python style question -- copying a data file

Dang Griffith dmgriffith at tasc.com
Wed Apr 23 17:53:48 CEST 2003


Guy Middleton wrote:

> Ok, here's another style question.
> 
> I want to copy a data file, but if the file doesn't already exist, I don't
> care.  I do care about any other errors, such as wrong permissions.
> 
> I have the following code, is this a typical Python way to do this?
> 
>     try:
>         os.stat(DATAFILE)
>         os.system("cp %s %s.new" % (DATAFILE, DATAFILE))
>     except OSError, arg:
>         # ENOENT is ok, we didn't do the copy
>         # anything else is a problem
>         if arg.errno != errno.ENOENT:
>             raise
> 

Look into the shutil module.  It has functions for copying files.
   --dang




More information about the Python-list mailing list