Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
To move a file in Python, you can use the shutil
module, which provides a high-level interface for file operations. The shutil.move()
function can be used to move a file from one location to another. Here's an example:
import shutil source_path = 'path/to/source/file.txt' destination_path = 'path/to/destination/file.txt' # Move the file from source_path to destination_path shutil.move(source_path, destination_path)
In this example, the file at source_path
will be moved to destination_path
. If the destination path points to an existing file, it will be overwritten. If the destination path points to a directory, the source file will be moved into that directory with its original name.
Keep in mind that if you are moving a file across different filesystems, shutil.move()
will first copy the file and then remove the original. If the operation fails, an exception (such as shutil.Error
or a more specific exception like FileNotFoundError
) will be raised. To handle these exceptions, you can use a try
and except
block:
import shutil source_path = 'path/to/source/file.txt' destination_path = 'path/to/destination/file.txt' try: shutil.move(source_path, destination_path) except shutil.Error as e: print(f"Error while moving file: {e}")
In this example, if an error occurs while moving the file, the error message will be printed, allowing you to take appropriate action.
Python move file to another directory:
shutil.move()
to move a file to another directory.import shutil source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/' shutil.move(source_path, destination_path)
shutil.move() function in Python:
shutil.move()
function for moving files.import shutil source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/' shutil.move(source_path, destination_path)
Move file in Python with os.rename():
os.rename()
to move a file by renaming it.import os source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/file.txt' os.rename(source_path, destination_path)
How to rename and move a file in Python:
os.rename()
to rename and move a file.import os source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/new_file.txt' os.rename(source_path, destination_path)
Move file to another folder using Python:
shutil.move()
.import shutil source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/folder/' shutil.move(source_path, destination_path)
Python move file with absolute path:
shutil.move()
.import shutil source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/' shutil.move(source_path, destination_path)
Atomic file move in Python:
shutil.move()
.import shutil source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/' shutil.move(source_path, destination_path)
shutil.copy2() vs shutil.move() in Python:
shutil.copy2()
and shutil.move()
for file operations.import shutil source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/' # Using shutil.copy2() shutil.copy2(source_path, destination_path) # Using shutil.move() shutil.move(source_path, destination_path)
Python move file if exists:
os.path.exists()
.import os import shutil source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/' if os.path.exists(source_path): shutil.move(source_path, destination_path)
Move and rename file in Python:
shutil.move()
.import shutil source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/new_file.txt' shutil.move(source_path, destination_path)
Python move file to different drive:
shutil.move()
.import shutil source_path = '/path/to/source/file.txt' destination_path = 'D:/path/to/destination/' shutil.move(source_path, destination_path)
Python move file and overwrite:
import shutil source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/new_file.txt' # Overwrite existing file if present shutil.move(source_path, destination_path)
Rename and move multiple files in Python:
import os import shutil source_folder = '/path/to/source/' destination_folder = '/path/to/destination/' for file_name in os.listdir(source_folder): source_path = os.path.join(source_folder, file_name) destination_path = os.path.join(destination_folder, 'new_' + file_name) shutil.move(source_path, destination_path)
Move all files in a directory using Python:
shutil.move()
.import os import shutil source_folder = '/path/to/source/' destination_folder = '/path/to/destination/' for file_name in os.listdir(source_folder): source_path = os.path.join(source_folder, file_name) destination_path = os.path.join(destination_folder, file_name) shutil.move(source_path, destination_path)
Python move file to a subdirectory:
shutil.move()
.import shutil source_path = '/path/to/source/file.txt' subdirectory = 'subfolder' destination_path = '/path/to/destination/' shutil.move(source_path, os.path.join(destination_path, subdirectory, 'file.txt'))
Move files based on a condition in Python:
shutil.move()
.import os import shutil source_folder = '/path/to/source/' destination_folder = '/path/to/destination/' for file_name in os.listdir(source_folder): source_path = os.path.join(source_folder, file_name) if os.path.isfile(source_path) and file_name.endswith('.txt'): destination_path = os.path.join(destination_folder, file_name) shutil.move(source_path, destination_path)
Atomic file operations in Python:
shutil.move()
for safe moves.import shutil source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/' shutil.move(source_path, destination_path)
Handle file move errors in Python:
import shutil source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/' try: shutil.move(source_path, destination_path) except Exception as e: print(f"Error: {e}")
How to move files safely in Python:
import shutil source_path = '/path/to/source/file.txt' destination_path = '/path/to/destination/' try: if os.path.isfile(source_path): shutil.move(source_path, destination_path) else: print("Source is not a file.") except Exception as e: print(f"Error: {e}")
Move files by extension in Python:
shutil.move()
.import os import shutil source_folder = '/path/to/source/' destination_folder = '/path/to/destination/' extension_to_move = '.txt' for file_name in os.listdir(source_folder): source_path = os.path.join(source_folder, file_name) if os.path.isfile(source_path) and file_name.endswith(extension_to_move): destination_path = os.path.join(destination_folder, file_name) shutil.move(source_path, destination_path)