How to move files from one directory to two other directories using if else conditions…
- User
How to move files from one directory to two other directories using if else conditions in Python
- Assistant
You can use the
osmodule in Python to achieve this. Here's an example code that demonstrates how to move files from one directory to two other directories based on certain conditions:pythonimport os # Get the list of files in the source directory files = os.listdir('source_directory') # Iterate over the files for file in files: # Check if the file is a directory if os.path.isdir(file): # Move the directory to the first destination directory os.rename(file, 'destination_directory_1/' + file) else: # Move the file to the second destination directory os.rename(file, 'destination_directory_2/' + file)In this code, we first use the
os.listdir()function to get the list of files in the source directory. Then, we iterate over each file in the list.For each file, we use the
os.path.isdir()function to check if it is a directory. If it is a directory, we use theos.rename()function to move it to the first destination directory by appending its name to the destination directory path.If the file is not a directory, we assume it is a regular file and move it to the second destination directory using the same approach.
By using this code, you can easily move files from one directory to two other directories based on their types.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.