Move a File in Python

  1. Move File in Python Using the shutil.move() Method
  2. Move Multiple Files in Python Using shutil.move() and listdir() Method
  3. Move Directory in Python Using the shutil.move() Method
  4. Move File in Python Using the os.rename() Method
  5. Move and Rename a File in Python Using the os.replace() Method

In Python programming, moving a file is a general operation. When we need to move some files to another folder, we can use the os utilities like shutil and move() to do it.

This article will introduce how to move a file in Python.

Move File in Python Using the shutil.move() Method

Let’s first import OS utility shutil using the below command.

import shutil

Now, we will use the move() function in the shutil utility.

shutil.move(source, destination)

Here, source and destination are file paths and of the string type.

Let’s breakdown the code into different steps:

The complete example code is as follows.

import shutil

source = r'C:\Test1\test.txt'
destination = r'C:\Test2\test.txt'

shutil.move(source,destination)

We must place r before the file path to make it as the raw string; otherwise, we need to escape the backslash, like C:\\Test\\test.txt.

The destination file is not necessary to be the same as the source file name. For example,

import shutil

source = r'C:\Test1\test.txt'
destination = r'C:\Test2\new.txt'

shutil.move(source,destination)

shutil.move() will raise a FileNotFoundError is the destination directory doesn’t exist.

Traceback (most recent call last):
...
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Test2\\test.txt'

shutil.move() overwrites the existing file if the destination file has existed.

Move Multiple Files in Python Using shutil.move() and listdir() Method

We can also move multiple files using the shutil.move() library and the os.listdir() function.

os.listdir() lists all the files in a directory, the for loop iterates each file, and shutil.move() moves the iterated file to the destination folder.

The example code is as follows:

import shutil
import os

source = r'C:\Test\file2'
destination = r'C:\Test\new file2'
files = os.listdir(source)

for file in files:
    new_path = shutil.move(f"{source}/{file}", destination)
    print(new_path)

Move Directory in Python Using the shutil.move() Method

This method will move one directory to another.

The example code is as follows:

import shutil
source = r'C:\Test\New folder'
destination = r'C:\Test\New folder'
shutil.move(source,destination)

Move File in Python Using the os.rename() Method

The example code is as follows:

import os
src=r'C:\Test1\test1.txt'
des=r'C:\Test2\test2.txt'
os.rename(src,des)

os.rename() method can also rename and move the file at the same time.

It will also raise the FileNotFoundError if the destination directory doesn’t exist.

Traceback (most recent call last):
...
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\Test1\test1.txt' -> 'C:\Test2\test2.txt'

Move and Rename a File in Python Using the os.replace() Method

os.replace() will rename the file or folder from source to destination. It will not work if the source and destination present on different filesystems.

The example code is as follows:

import os

src=r'C:\test1\test1.txt'
des=r'C:\test2\test2.txt'

os.replace(src,des)
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python File

  • Open All the Files in a Directory in Python
  • Read Binary File in Python