[Tutor] file move with wait period

banda gunda bandagunda at hotmail.com
Fri Aug 4 16:03:35 EDT 2017


Thanks Cameron (I hope I address your first name correctly).
Get Outlook for iOS<https://aka.ms/o0ukef>
________________________________
From: Cameron Simpson <cs at cskk.id.au>
Sent: Friday, August 4, 2017 5:18:58 AM
To: banda gunda
Cc: tutor at python.org
Subject: Re: [Tutor] file move with wait period

On 03Aug2017 11:22, banda gunda <bandagunda at hotmail.com> wrote:
>Dear tutors,
>
>I am interested to move a set of files, periodically, from source_folder to dest_folder.
>source_folder receives processed files from a ‘decompressing program’.  The files are deposited from the ‘decompressing program’ at periodic random intervals.  Furthermore some of the files dropped in the source_folder are large, of typical size 800MB.  These files take time before it is completely deposited in the source_folder.
>
>I could use shutil.move (source_folder, dest_folder).  But then how?

You'd normally use shutil.move(source_file, dest_file) i.e. move individual
files. Or os.rename, but that requires that the source and dest are the same
filesystem.

>The frequency of moving files from source_folder to dest_folder is flexible.  It could be once in 15 minutes (or even lengthier).
>Challenge for me:  How could the code include the wait statement to make sure that the file in the source_folder is completely formed before attempting to move to dest_folder?
>Thanks in advance for your comments/guidance.

That is the tricky thing; this shows up a lot, also with FTP upload locations
and so forth.

The problem, as you state, is knowing when the file is complete. There are 2
basic approaches: do not put a fil into the transfer directory before it is
complete, or to monitor the files for changes. On the premise that the file
will be being actively written until it is complete you could keep a list if
the files in the directory. For each name, record the file's size and
modification time. Wait for that to be unchanged "long enough"; you might then
decide it is ready to move.

Cheers,
Cameron Simpson <cs at cskk.id.au> (formerly cs at zip.com.au)


More information about the Tutor mailing list