Mikes1962 Mikes1962 - 1 year ago 71
Python Question

Know when directory copy is complete

I have a data analysis application in Python running on Windows. I want users to have the capability to copy a folder containing data files into an "in box" folder, at which point my application will begin processing the data. My question is does Windows provide an event when the directory copy is complete


Answer Source

There's probably a better way to do it but you could bodge it a bit by counting the amount of files in the folder to start with, then waiting until there's an increase in this amount, like so:

import glob, os, time
directory = os.path.join('inbox','*') #Uses wildcard so ANY files in the 'inbox' directory are used.
numFiles = len(glob.glob(directory)) #Gets initial number of files
while True:
    numFilesNew = len(glob.glob(directory)) #Gets current number of files
    if numFilesNew > numFiles: #Compare current amount of files to initial 
        break #Exit loop if there's an increase 
    else: time.sleep(1) #Wait if no files were added
files = glob.glob(directory) #Make a list of filenames in the directory

For those unsure, glob.glob essentially returns a list of files in a given directory.

This code was written in Python 3.4 but I don't see a reason why it wouldn't work in other versions.

If you wanted only files of a certain extension then you could swap out

directory = os.path.join('inbox','*')


directory = os.path.join('inbox','*.png')

But of course change '.png' for whatever file extension you need. Of course, that's not to say the file in question is actually of that format/type, only that it has the extension.

This is my first answer on this site, hope it helps! :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download