shutil.copy2 error

Karthik Gurusamy kar1107 at gmail.com
Tue Sep 25 00:34:44 CEST 2007


On Sep 24, 7:34 am, Horse <jbutu... at gmail.com> wrote:
> I've written a python script that copies a nightly Oracle backup file
> to another server.  Every couple days, the script fails with this
> error message:
>
> Error copying Q:/Oradata/GISPROD/Backups/3UISN35R_1_1 to s:/gisprod/
> backups/3UISN35R_1_1
> [Errno 22] Invalid argument
>
> Here's the code for the function I'm running.  The path names are all
> correct, and it works *most of the time*.  It only fails about once or
> twice a week.  Anyone know where I can get more info on this "errno 22
> invalid argument"?
>
> def CopyNewFiles(SOURCE_DIR, DEST_DIR):
>     global STATUS
>     try:
>         os.chdir(SOURCE_DIR)
>         for x in os.listdir(SOURCE_DIR):
>             int_time = os.stat(x)[stat.ST_CTIME]
>             str_time = time.ctime(int_time)
>             if datetime.date.fromtimestamp(int_time + 0.00) >
> YESTERDAY:
>                 try:
>                     DEST_FILE = os.path.join(DEST_DIR, x)
>                     logfile.write("     Copying " + SOURCE_DIR + x + "
> to " + DEST_FILE + "\n")
>                     logfile.flush()
>                     if not os.path.isfile(DEST_FILE):
>                         shutil.copy2(x, DEST_FILE)

I'm not sure of the error; but one possibility is that the source-
directory contents may be modified by some other process, while you
are looping here copying one-by-one the files.

So a file 'x' could be present at the time you enter loop, but gone by
the time you try the shutil.copy2. Again this is just a guess...

Yet another possibility is 'x' is not a regular file (say in unix it
could be a named pipe).. you can try adding a check for x (like
os.path.isfile(x)) and copy only regular files.

Karthik

>                     else:
>                         logfile.write("        File exists.  Skipping.
> \n")
>                         logfile.flush()
>                 except (IOError, os.error), why:
>                     logfile.write("\n\nError copying " + SOURCE_DIR +
> x + " to " + DEST_FILE + "\n\n")
>                     logfile.write("\n" + str(why) + "\n")
>                     logfile.flush()
>                     STATUS = "FAILED"
>     except:
>         logfile.write("\n\nUnhandled error in CopyNewFiles\n\n")
>         logfile.write("SOURCE_DIR = " + SOURCE_DIR + "\n")
>         logfile.write("DEST_DIR = " + DEST_DIR + "\n")
>         logfile.flush()
>         STATUS = "FAILED"





More information about the Python-list mailing list