FileNotFoundError when attempting to move files into a temp directory

1k views Asked by At

I am attempting to move files inside of a subdirectory into a temp folder. Here is my code:

with tempfile.TemporaryDirectory() as tempDirectory:
   for root, dirs, files in os.walk(fileDestination, topdown=True):
        for file in files:
            shutil.move(file, tempDirectory)

When I look at my debugger I can see the values of the file variable holding the files I want to move. But nothing moves and I am then given the error FileNotFoundError that references the file I want to move. When I look into my file explorer I can see that the file did not move.

1

There are 1 answers

0
dhebert-dev On

Figured it out on my own. So even though file variable was holding the filename it was not holding the entire path to the file. The below code works:

for subdir, dirs, files in os.walk(fileDestination, topdown=True):
        for file in files:
            fileName = os.path.join(fileDestination+"\\"+file)
            print(fileName)
            shutil.move(fileName, tempDirectory)