Unknown model· unknown
from NVIDIA HelpSteer3 Preference
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
To move files from one directory to two other directories using if else conditions in Python, you can use the
shutilmodule for file operations andosmodule for directory operations. Here's a step-by-step guide:- Import the required modules:
pythonimport os import shutil- Define the source directory and the two destination directories:
pythonsource_dir = "/path/to/source/directory" dest_dir1 = "/path/to/destination/directory1" dest_dir2 = "/path/to/destination/directory2"- Create a function to move files based on a condition. In this example, we'll use the file size as the condition. You can modify the condition according to your needs.
pythondef move_files(source_dir, dest_dir1, dest_dir2, size_threshold=1000): """ Move files from source_dir to dest_dir1 or dest_dir2 based on file size. Files larger than size_threshold will be moved to dest_dir1, others to dest_dir2. """ # Ensure directories exist os.makedirs(dest_dir1, exist_ok=True) os.makedirs(dest_dir2, exist_ok=True) # Iterate through files in the source directory for filename in os.listdir(source_dir): file_path = os.path.join(source_dir, filename) # Check if it's a file if os.path.isfile(file_path): file_size = os.path.getsize(file_path) # Move file based on size condition if file_size > size_threshold: shutil.move(file_path, dest_dir1) else: shutil.move(file_path, dest_dir2)- Call the function with the specified directories and size threshold:
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.