edit the python script to be able to let me choose the destination filename def…
- User
edit the python script to be able to let me choose the destination filename
def move_wav_file(src_path, dst_path): """ Moves a .wav file from the source directory to the destination directory. :param src_path: The full path to the source .wav file. :param dst_path: The full path to the destination directory. """
Check if the source file exists
if not os.path.isfile(src_path): print(f"Source file '{src_path}' does not exist.") return
Check if the destination directory exists, if not, create it
if not os.path.isdir(dst_path): os.makedirs(dst_path) print(f"Destination directory '{dst_path}' created.")
Get the base name of the file
file_name = os.path.basename(src_path)
Construct the full destination path
dst_file_path = os.path.join(dst_path, file_name)
Move the file
try: shutil.move(src_path, dst_file_path) print(f"File '{file_name}' moved successfully to '{dst_path}'.") except Exception as e: print(f"An error occurred while moving the file: {e}")
- Assistant
To allow for specifying a destination filename, you need to modify the script so that it accepts an additional parameter for the destination file name. Here's how you can do it:
import os import shutil def move_wav_file(src_path, dst_dir, dst_filename=None): """ Moves a .wav file from the source directory to the destination directory. :param src_path: The full path to the source .wav file. :param dst_dir: The full path to the destination directory. :param dst_filename: The desired name for the file in the destination directory. """ # Check if the source file exists if not os.path.isfile(src_path): print(f"Source file '{src_path}' does not exist.") return # Check if the destination directory exists, if not, create it if not os.path.isdir(dst_dir): os.makedirs(dst_dir) print(f"Destination directory '{dst_dir}' created.") # Use the provided destination filename or default to the source filename if dst_filename is None: dst_filename = os.path.basename(src_path) # Construct the full destination path dst_file_path = os.path.join(dst_dir, dst_filename) # Move the file try: shutil.move(src_path, dst_file_path) print(f"File moved successfully to '{dst_file_path}'.") except Exception as e: print(f"An error occurred while moving the file: {e}") # Example usage: # move_wav_file('path/to/source.wav', 'path/to/destination', 'new_name.wav')
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.