Skip to content

How to Move a File in Python? AlixaProDev Spark By {Examples}

  • by

There are different ways to move a file in Python from one directory to another directory or from one file to another file. Some methods are used to preserve the original file’s metadata, such as file permissions and timestamps. In this article, we will learn these different methods of moving files in Python with examples.

Related: File Handling with Examples

1. Quick Examples of Moving a File

These quick examples will give you a high-level idea of how to move a file in python. We will discuss each of these methods in detail.

# Quick Examples of Moving a File

# Imports
import shutil
import os
from pathlib import Path
import subprocess

# Example file paths
src = ‘/old_dir/file’
dst = ‘/new_dir’

# Method 1: Using shutil.move() function
shutil.move(src, dst)

# Method 2: Using os.rename() function
os.rename(src, dst)

# Method 3: Using os.replace() function
os.replace(src, dst)

# Method 4: Using shutil.copy2() function
shutil.copy2(src, dst)
os.remove(src)

# Method 5: Using Path.rename() method
src_path = Path(src)
dst_path = Path(dst)
src_path.rename(dst_path)

2. Use shutil.move() to Move a File in Python

To move a file in Python use the move() function from the shutil module. The shutil module provides a higher-level interface for file operations. The move() function is used to move a file or directory tree from one directory to another, while also preserving its metadata.

The metadata of a file or directory is file permissions, timestamps, etc. The shutil.move() actually copies a file followed by a deletion of the original file or directory. The copy operation ensures that the metadata is preserved along with the file content.

The syntax for using the shutil.move() function is as follows:

# Syntax
shutil.move(src, dst)

Following is an example of moving a file from the source to the destination directory.

# Using the shutil.move()
import shutil

# Source file
src = ‘file.txt’

# Destination file
dst = ‘./new/newfile.txt’

# Move file
shutil.move(src, dst)

If the destination directory does not exist, the shutil.move() function will create a new directory and move the file to the new name.

The above example moves the file from the Python current directory to the destination directory and the file name is changed to the specified name.

3. os.rename() – Move File in Python

The os.rename() function allows you to rename a file or directory by specifying its old name and a new name. While it is primarily used for renaming files, it can also be used for moving files by renaming the file to its new location.

# Using the os.rename() to move a file
import os

src = ‘file.txt’
dst = ‘./new/newfile.txt’

# Move file using os.rename()
os.rename(src, dst)

4. Use os.replace() to Move a File

The os.replace() function is a built-in function in Python os module that allows you to replace or move a file by specifying its source and destination paths. This function is similar to the shutil.move() function, but it overwrites the destination file if it already exists.

Here is an example of how to use the os.replace() function to move a file from one directory to another and replace the destination file if it already exists:

# Using the os.replace() function
import os

src = ‘file.txt’
dst = ‘./new/newfile.txt’

# Move file using os.replace()
os.replace(src, dst)

5. Path.rename()

The Path.rename() method is a built-in method in Python’s pathlib module that allows you to rename a file or directory by specifying its old name and a new name. While its primary purpose is to rename files, it can also be used to move files by renaming them with a new path.

The syntax for using the Path.rename() method is as follows:

# Syntax of the path.rename()
path.rename(new_name)

Below are the examples of using this method.

# Using the pathlib module to move
from pathlib import Path

# Source
src = ‘file.txt’
src_path = Path(src)

# Destination
dst = ‘./new/newfile.txt’
dst_path = Path(dst)

# MOve by renaming a file
src_path .rename(dst_path )

6. subprocess Module

The subprocess module in Python allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. While its primary purpose is to run external commands, it can also be used to move files by calling operating system commands.

# Syntax of the subprocess.run()
import subprocess

subprocess.run([‘mv’, src, dst])

See the example of using subprocess module to move a file in python:

# Subprocess module to move a file
import subprocess

src = ‘file.txt’
dst = ‘./new/newfile.txt’

subprocess.run(f’mv {src} {dst}’, shell=True)

7. Summary and Conclusion

In this article, you have learned how to move a file in python using different methods. You can now use any of these methods of your choice. Some of the methods retain the meta data of the file and some methods rename the file after copy. I hope this article was helpful. Leave questions in the comment section.

Happy Coding!

 There are different ways to move a file in Python from one directory to another directory or from one file to another file. Some methods are used to preserve the original file’s metadata, such as file permissions and timestamps. In this article, we will learn these different methods of moving files in Python with examples.  Read More Python, Python Tutorial, Python File Handling 

Leave a Reply

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