Moving Files Using Python – Tutorial with Examples

Moving Files Using Python

Moving files from one location to another is another common task in programming. Python provides several methods to move files from one location to another. In this article, we will discuss some of the most commonly used methods for moving files in Python.

Method 1: Using shutil Module

The shutil module in Python provides a high-level interface for moving files and directories. It has several functions for moving files, directories, and even entire directory trees. The most commonly used function for moving files is shutil.move(). This function moves the file from the source path to the destination path.

Here is an example of how to use the shutil.move() function:

import shutil
# Specify the source and destination paths
src_path = 'C:/Users/User/Documents/example.txt'
dst_path = 'C:/Users/User/Desktop/example.txt'

# Use shutil.move() to move the file from source to destination
shutil.move(src_path, dst_path)

print('File moved successfully!')

In the above example, we imported the shutil module and specified the source and destination paths using variables src_path and dst_path. We then used the shutil.move() function to move the file from the source path to the destination path. Finally, we printed a message to confirm that the file was moved successfully.

Method 2: Using os Module

The os module in Python provides a lower-level interface for moving files. It has several functions for file operations, including moving files. The most commonly used function for moving files in the os module is os.rename(). This function moves the file from the source path to the destination path and overwrites the destination file if it already exists.

Here is an example of how to use the os.rename() function:

import os
# Specify the source and destination paths
src_path = 'C:/Users/User/Documents/example.txt'
dst_path = 'C:/Users/User/Desktop/example.txt'

# Use os.rename() to move the file from source to destination
os.rename(src_path, dst_path)

print('File moved successfully!')

In the above example, we imported the os module and specified the source and destination paths using variables src_path and dst_path. We then used the os.rename() function to move the file from the source path to the destination path. Finally, we printed a message to confirm that the file was moved successfully.

Method 3: Using subprocess Module

The subprocess module in Python allows you to execute external commands and programs. It can be used to run the mv command on Unix/Linux systems or the move command on Windows systems to move files from one location to another.

Here is an example of how to use the subprocess module to move a file:

import subprocess
# Specify the source and destination paths
src_path = 'C:/Users/User/Documents/example.txt'
dst_path = 'C:/Users/User/Desktop/example.txt'

# Use subprocess.run() to run the mv command and move the file
subprocess.run(['mv', src_path, dst_path])

print('File moved successfully!')

In the above example, we imported the subprocess module and specified the source and destination paths using variables src_path and dst_path. We then used the subprocess.run() function to execute the mv command on Unix/Linux systems to move the file from the source path to the destination path. Finally, we printed a message to confirm that the file was moved successfully.

Method 4: Using Pathlib Module

The Pathlib module in Python provides an object-oriented interface for file operations. It can be used to move files by calling the rename() method of the Path object.

Here is an example of how to use the Pathlib module to move a file:

from pathlib import Path
# Specify the source and destination paths
src_path = Path('C:/Users/User/Documents/example.txt')
dst_path = Path('C:/Users/User/Desktop/example.txt')
# Use Path.rename() to move the file from source to destination
src_path.rename(dst_path)

print('File moved successfully!')

In the above example, we imported the Path class from the pathlib module and specified the source and destination paths using Path() function. We then used the rename() method of the src_path object to move the file from the source path to the destination path. Finally, we printed a message to confirm that the file was moved successfully.

Method 5: Using FTP

If you want to move a file to a remote location over the internet, you can use the ftplib module in Python. This module provides a way to connect to an FTP server and perform various operations, including uploading and downloading files.

Here is an example of how to use the ftplib module to move a file to a remote FTP server:

import ftplib
# Specify the FTP server details
ftp = ftplib.FTP('ftp.example.com')
ftp.login('username', 'password')
# Specify the source and destination paths on the FTP server
src_path = 'example.txt'
dst_path = '/public_html/example.txt'

# Use ftp.cwd() to change the working directory
ftp.cwd('/public_html')

Use ftp.storbinary() to upload the file to the FTP server
with open(src_path, 'rb') as f:
    ftp.storbinary('STOR ' + dst_path, f)

ftp.quit()
print('File moved successfully!')

In the above example, we imported the ftplib module and specified the FTP server details using the FTP() function. We then logged in to the FTP server using the login() function. We specified the source and destination paths on the FTP server using variables src_path and dst_path. We changed the working directory on the FTP server using the cwd() function. Finally, we uploaded the file to the FTP server using the storbinary() function and printed a message to confirm that the file was moved successfully.

Leave a Reply

Your email address will not be published. Required fields are marked *