gpt-4-0125-preview· OpenAI
WildChat-4.8M (AI2)
i have folder folder of files i want a python code to iterate files of folder A and for…
- User
i have folder folder of files i want a python code to iterate files of folder A and for each filename of folder A if file name dosent exist in folder B then move it to folder C give me proper python code
- Assistant
Certainly! Below is a Python script that accomplishes what you're asking for. Before running the code, please ensure you replace
'path/to/folderA','path/to/folderB', and'path/to/folderC'with the actual paths to your Folder A, Folder B, and Folder C, respectively.import os import shutil # Define the paths to the folders folderA_path = 'path/to/folderA' folderB_path = 'path/to/folderB' folderC_path = 'path/to/folderC' # Iterate through each file in folder A for filename in os.listdir(folderA_path): fileA = os.path.join(folderA_path, filename) fileB = os.path.join(folderB_path, filename) # If the file doesn't exist in folder B, move it to folder C if not os.path.exists(fileB): dest_path = os.path.join(folderC_path, filename) shutil.move(fileA, dest_path) print(f"Moved '{filename}' from Folder A to Folder C.") print("Operation completed.")