In Python, you can use the os
Modular rename()
Function or shutil
Modular move()
Function to change the path of the file. The following are sample codes for two methods:
- use
os.rename()
Function changes file path:
import os original_path = "/path/to/original/file" new_path = "/path/to/new/location/new_file_name"
os.rename(original_path, new_path)
Please set /path/to/original/file
Replace with the actual path of the original file where you want to change the path /path/to/new/location/new_file_name
Replace with the new path of the changed file.
- use
shutil.move()
Function changes file path:
import shutil original_path = "/path/to/original/file" new_path = "/path/to/new/location/new_file_name"
shutil.move(original_path, new_path)
Similarly, please /path/to/original/file
Replace with the actual path of the original file where you want to change the path /path/to/new/location/new_file_name
Replace with the new path of the changed file.
When using these functions, ensure that the original file exists and that you have sufficient permissions to change the file path. If the target path already exists, the files under the path will be overwritten. Therefore, before changing the file path, be sure to perform appropriate backup operations to avoid data loss.