1

I'm trying to make a code that can move files from one folder to another. For instance, I have files named 0001.jpg, 0002.jpg ... and so on in /test1/ folder and want to move those files to /test3/ folder if the same file name doesn't exist in /test2/. So, if there's 0001.jpg both in folder /test1/ and /test2/ the file in /test1/ won't be moved to /test3/ folder but if there's 0002.jpg in /test1/ and not in /test2/, it moves to /test/3.

I've tried to write the code on my own but it won't work. Can you please help with this? Thanks in advance!

import os
import shutil

def Move_files(root_path, refer_path, out_path) :
    root_path_list= [file for file in os.listdir(root_path)]
    refer_path_list= [file for file in os.listdir(refer_path)]

    for file in root_path_list:
        if refer_path_list in root_path_list:
            shutil.move(os.path.join(os.listdir(root_path, file)),os.path.join(os.listdir(refer_path, file)))

if __name__ == '__main__' :
    Move_files("D:\\Dataset\\test1", "D:\\Dataset\\test2", "D:\\Dataset\\test3")
tdelaney
  • 73,364
  • 6
  • 83
  • 116
lemi
  • 101
  • 1
  • 2
  • 6

3 Answers3

2

Updated: You can check if the file exists in your other directory using os.path.exists, and then only moving it if it does not already exist in /test2/:

if not os.path.exists(os.path.join(refer_path, file)):
    shutil.move(os.path.join(os.listdir(root_path, file)),os.path.join(os.listdir(refer_path, file)))

Also, os.listdir only accepts one argument, the path to the directory of which you want to list the files. I think you want to change your shutil.move statement to this: shutil.move(os.path.join(root_path, file),os.path.join(out_path, file))

awarrier99
  • 3,628
  • 1
  • 12
  • 19
1

Try this

import os
import shutil

def Move_files(root_path, refer_path, out_path) :
    root_path_list= [file for file in os.listdir(root_path)]
    refer_path_list= [file for file in os.listdir(refer_path)]

    for file in root_path_list:
        if file not in refer_path_list:
            shutil.move(os.path.join(os.listdir(root_path, file)),os.path.join(os.listdir(out_path, file)))

if __name__ == '__main__' :
    Move_files("D:\\Dataset\\test1", "D:\\Dataset\\test2", "D:\\Dataset\\test3")
Nandu Raj
  • 2,072
  • 9
  • 20
1

You can use set to find the difference between the file lists. I added an isfile check to ignore subdirectories (e.g., the "." and ".." directories in linux) and since shutil.move accepts a target directory, there is no need to build the target file name.

import os
import shutil

def Move_files(root_path, refer_path, out_path) :
    root_files = set(filename for filename in os.listdir(root_path)
        if os.path.isfile(filename))
    refer_files = set(filename for filename in os.listdir(refer_path)
        if os.path.isfile(filename))
    move_files = root_files - refer_files

    for file in move_files:
        shutil.move(os.path.join(root_path, file), out_path)

if __name__ == '__main__' :
    Move_files("D:\\Dataset\\test1", "D:\\Dataset\\test2", "D:\\Dataset\\test3")
tdelaney
  • 73,364
  • 6
  • 83
  • 116