-1

I have a large folder with lots of subfolders, each of which contains one to several files. I'd like to move some of these files based on their filename. I have a list containing the filenames of the files I want to move, so basically I would like to check for every file in the mither directory if it's name's on the list and if so - move it to the new directory.

I wrote the following script, but unfortunately it doesn't work. Does anyone have an idea how to fix it, or a better suggestion for a script that would perform the desired function?

import os
import shutil

curr_fold = "/Users/ruthersh/Alice/Bacterial_seqs/FAA"
dest = "/Users/ruthersh/Alice/Bacterial_seqs/Plasmids"

for (dirname, dirs, files) in os.walk(curr_fold):
    for filename in files:
        if (filename[:9]) in NCS:
            src = os.path.realpath(filename)
            shutil.move(src, dest)
ballade4op52
  • 2,142
  • 5
  • 27
  • 42
Alice312
  • 13
  • 3

1 Answers1

1

From Python Standard Library documentation on os.walk : Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name). So you should have

src = os.join(dirname, filename)

If something goes wrong, please add some print statements to verify the values of src, and eventually dest to see what you really ask to shutils.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252