Search code examples
pythonglob

Selecting File by Latest File Modified and Moving


I'm trying to get my code to select the most recent file in a folder and move it into a different folder:

#Pull Files from 'to_be_loaded' - oldest files first
list_of_files = glob.glob('D:/_to be loaded/')
latest_file = max(list_of_files, key=os.path.getctime)
print (latest_file)
dest1 = 'D:/Datasheet Loading'
shutil.move(latest_file, dest1)

The problem that I am having is that it is moving the folder itself (to_be_loaded) and not the contents, I assumed the code would filter/move on the files inside rather than the folder itself.

How would I change it so that this doesn't happen?


Solution

  • Glob does a regex match, so it's doing what you ask. Try glob.glob('D:/_to be loaded/*')