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