Python provides numerous functions to deal with files and directories and to perform operations on them. To move files or directories in Python, the “shutil.move()” function of the “shutil” module and the “os.rename()” function of the os module, etc., are used.
This post will provide various ways to move files or directories in Python using numerous examples.
- Method 1: Using shutil.move() Function
- Method 2: Using os.rename() Method
- Method 3: Using Pathlib Module
Method 1: Using shutil.move() Function
The “shutil.move()” function is used to move the specified file or directory from one location/path to another. The syntax of the “shutil.move()” function is demonstrated below:
shutil.move(source_path, destinaiton_path, copy_function=copy2)
From the above syntax:
- The parameter “source_path” represents the source path of the file or directory to be moved.
- The parameter “destination_path” represents the destination path where the source file will move.
- The optional parameter “copy_function” specifies the function used to copy the file. The “shutil.copy2()” function is used by default in this parameter.
Example 1: Move the File to Specified Path
The file is currently present at the following path:
To move the file to the specified path, the below code is used in Python:
Code:
import shutil
orginal_path = r"C:\Users\p\Documents\program\ILf\example.txt"
final_path = r"D:\itslinuxfoss\example.txt"
shutil.move(orginal_path, final_path)
- The source path is assigned to a variable named “orginal_path”.
- The destination path is assigned to a variable named “final_path”.
- The “shutil.move()” function accepts the original and destination path as an argument and moves the file to a specified location/path.
Output:
The file named “example” has been moved to the specified location.
Example 2: Move the Similar File By Renaming the File’s Name
The following code is used to move the file to a specific location by renaming the name of the file:
Code:
import os
import shutil
orginal_path = r"C:\Users\p\Documents\program\ILf\example.txt"
final_path = r"D:\itslinuxfoss\\"
if os.path.exists(final_path + 'example.txt'):
value = os.path.splitext('example.txt')
output = value[0] + '_new' + value[1]
shutil.move(orginal_path, os.path.join(final_path, output))
else:
shutil.move(orginal_path, final_path + 'example.txt')
- The “os” and “shutil” modules are imported.
- The file’s source path is assigned to a variable named “orginal_path”
- The destination path of the file is assigned to a variable named “final_path”.
- The “if” statement is utilized along with the “os.path.exists()” function to check whether the file is present in the destination path or not.
- If the file with the same name is present in the destination directory, then the file’s name will be renamed and moved to the destination path.
- If the file does not exist, then the “shutil.move()” function is used in the “else” statement to move the files to a specific path.
Output:
The file named “example.txt” has been renamed “example_new.txt” while moving from the source/original path to the destination path.
Example 3: Move All Files to Specific Path
Multiple files are placed at the following path:
The below code is used to move all files to a specific path:
Code:
import os
import shutil
orginal_path = r"C:\Users\p\Documents\program\ILf\\"
destination_path = r"D:\itslinuxfoss\\"
for file in os.listdir(orginal_path):
source_all = orginal_path + file
output = destination_path + file
if os.path.isfile(source_all):
shutil.move(source_all, output)
print('Files Moved:', file)
- The “os” and “shutil” are imported at the beginning of the program.
- The source path and destination path are given to the variable named “orginal_path‘ and “destination_path”.
- The “os.listdir()” function lists all the files and folders of the source directory.
- The “for loop” iterates over the listed directory and returns only files excluding directories.
- All the files placed at the original path are stored in a variable named “source_all”.
- The “if” statement is utilized with the “os.path.isfile()” function to select only the files placed at the source path.
- The “shutil.move()” function is used to move all the files placed at the source path to the destination path.
Output:
The above snippet shows that all the files have been moved from the source path to the destination path.
Example 4: Move Multiple Files to Specific Path
To move multiple files to a specific path, the below code is used in Python:
Code:
import os
import shutil
orginal_path = r"C:\Users\p\Documents\program\ILf\\"
destination_path = r"D:\itslinuxfoss\\"
move_file = ['sample1.txt', 'sample3.txt']
for file in move_file:
source_all = orginal_path + file
output = destination_path + file
if os.path.isfile(source_all):
shutil.move(source_all, output)
print('Files Moved:', file)
- All the code remains the same as in the previous example. The only difference is that specific file names are passed to a list this time.
- Next, the for loop iterates over the list and moves the multiple files from the source/original path to the final/destination path.
Output:
The specific multiple files are moved from one path to another.
Example 5: Move Directory to Specified Path
The source path of the file is shown below:
The following code is used to move the directory to the specified path:
Code:
import shutil
orginal_path = r"C:\Users\p\Documents\program\ILf"
final_path = r"D:\itslinuxfoss"
shutil.move(orginal_path, final_path)
- The source path and destination path of the directory is assigned to a variable named “orginal_path” and “final_path”.
- The “shutil.move()” function accepts the original and final path as an argument and moves the directory to the specified path.
The specific directory has been moved from one place to another.
Method 2: Using os.rename() Method
The “os.rename()” method is used to move the file or directory to the specified path. Here is an example code:
Code:
import os
orginal_path = r"C:\Users\p\Documents\program\ILf\sample1.txt"
final_path = r"C:\Users\p\Documents\program\ILf_NEW\sample1.txt"
os.rename(orginal_path, final_path)
The “os.rename()” function accepts the source path and destination path as an argument and moves the file.
Output:
The file has been moved from one path to another.
Method 3: Using Pathlib Module
The “pathlib.path()” function of the “pathlib” module is used in the below code to move the files or directory:
Code:
import pathlib
orginal_path = r"C:\Users\p\Documents\program\ILf\sample1.txt"
final_path = r"C:\Users\p\Documents\program\ILf_NEW\sample1.txt"
pathlib.Path(orginal_path).rename(final_path)
- The “pathlib.path()” function is used along with the “rename()” function using dot syntax.
- The “pathlib.path()” accepts the source path as an argument, and the “rename()” function accepts the “destination path” as an argument.
- Both these functions are utilized to move the source/original file to the specified destination path.
Output:
The file has been moved from one path to another.
Conclusion
To move files or directories the “shutil.move()” function, “os.rename()” method, and “pathlib.path()” function is used in Python. The “shutil.move()” function is used to move single, multiple, or all files to a specified Path. The “shutil.move()” function is also used to move the directory to the specified path. Similarly to move the files or directory the “os.rename()” and “pathlib.path()” are used in Python. This post presented an in-depth guide on how to move files or directories in Python.