apples-oranges
apples-oranges

Reputation: 987

Python move two most recent files to new directory

I want to iteratively move the two most recent files, each based in separate folders, to a new folder and spawn a subprocess. Once this subprocess is finished, the second most recent files move to the destination folder, and go in the subprocess etc.

I have a function which sorts the files in a folder based on the creation date, this works. But when I try to move them using shutil, I get an error (IOError: [Errno 2] No such file or directory: 'file_name.jpg').

import subprocess
import shutil
import os

#destination folder
dest = '/home/itsme/C'

#sort files in source folders A and B
def sorted_ls(path):
    ctime = lambda f: os.stat(os.path.join(path, f)).st_ctime
    return sorted(os.listdir(path), key=ctime)

ordered_list_A = list(sorted_ls('/home/itsme/A'))
ordered_list_B = list(sorted_ls('/home/itsme/B'))

#move two most recent files to new dest: Problem!
for i in ordered_list_A:
    shutil.move(i, dest)

    for j in ordered_list_B:
        shutil.move(j, dest)


#here comes some code to put the two newest files in 
#subprocess.call which I haven't figured out yet

Upvotes: 1

Views: 1071

Answers (1)

Alex Alifimoff
Alex Alifimoff

Reputation: 1849

You're not including the full filepath in the move command, just the name of the file, so you're only looking in the current directory. You need to join /home/itsme/* with the actual filename. You can do this in your sorted_ls command.

Upvotes: 1

Related Questions