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")
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))