Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Move File or Folder in Python

  • 27 Nov, 2025
  • Com 0
Moving a File or Folder in Python

Moving files means changing the file’s location within the file system from one location to another without changing its content.

Here are three ways to move a file in Python

  1. Using the shutil.move()
  2. Using os.rename()
  3. Using the pathlib.Path()

Method 1: Using shutil.move()

The shutil.move() function from the shutil (shell utilities) module is more versatile than os.rename(). It allows moving files between different filesystems and automatically renames files when the destination is a directory.

It’s a good choice for more complex file operations. 

It works for both files and folders. However, it silently overwrites the existing destination file.

Moving a file

Let’s move a file from one folder to another folder. Before moving a file, our file.txt path relative to the current working directory is dir/newdir/file.txt:

Source directory before moving

We will move this file inside the direct “dir” folder:

import shutil

source_file_path = "dir/newdir/file.txt"
destination_file_path = "dir/file.txt"

# Moving a file by using shutil.move()
shutil.move(source_file_path, destination_file_path)

print('The file is successfully moved to another destination')

# Output: The file is successfully moved to another destination

Moved file

Moving an entire directory

Let’s move a directory using shutil.move() method by passing the source and destination folders.

Before moving a directory, our current directory looks like this:

Current snapshot

 

Now, we will move the directory called “info” to the “csv/info_backup” folder.

import shutil

source_dir_path = "newdir/data/info"
destination_dir_path = "csv/info_backup"

# Moving a directory by using shutil.move()
shutil.move(source_dir_path, destination_dir_path)

print('The folder is successfully moved to another destination')

# Output: The folder is successfully moved to another destination

Here is the moved directory screenshot:

moved directory screenshot

Moving multiple files

You can use the os.listdir() function to list all files in a directory, or you can manually create a list of file names if the files are not all in the same directory.

Use a loop to iterate over the list of files, and use shutil.move() to move each file to the desired destination.

multiple source files

import os
import shutil

source_dir = "dir/newdir"

destination_dir = "dir"

files_to_move = os.listdir(source_dir)

for file_name in files_to_move:
    source_file = os.path.join(source_dir, file_name)
    destination_file = os.path.join(destination_dir, file_name)

    # Moving the file
    shutil.move(source_file, destination_file)

Here is the output:

Moved multiple filesMake sure that the destination directory exists before running the script.

 

Also, handle exceptions as necessary, especially when files may already exist in the destination directory or the source file does not exist.

Method 2: Using os.rename()

The os.rename() function from the os module renames a file or directory, including moving it to a different directory. It is straightforward but is limited to moving within the same filesystem.

If the file is not found in the provided path, it will return the FileError.

Here is the file.txt file, which we will move to a different destination:

src file

import os

# Moving a file by renaming it's path
os.rename('dir/file.txt', 'dir/newdir/file.txt')

print('The file is successfully moved to another destination')

# Output: The file is successfully moved to another destination

After moving a file to a new destination:

moving dst file

Method 3: Using pathlib.Path.rename()

The pathlib module, introduced in Python 3.4, provides an object-oriented approach to handling filesystem paths. The main advantage is that it is well-suited to scripts that handle many file paths.

Using Path() objects, you can move a file with the rename() method, which works similarly to os.rename() but in an object-oriented way.

Here is our source file “file.txt” inside the “dir” folder:

Screenshot of source file file.txt inside the dir folder

We will move the “file.txt” file to the new destination, which is the “newdir” directory:

from pathlib import Path

source_file_path = Path("dir/file.txt")
destination_file_path = Path("dir/newdir/file.txt")

# Move a file by using source_file_path.rename()
source_file_path.rename(destination_file_path)

print('The file is successfully moved to another destination')

# Output: The file is successfully moved to another destination

Output screenshot of moved a file using the pathlib module (Python 3.4+)

That’s all!

Post Views: 2
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Create List of Numbers with Given Range in Python
How to Flush the Output of the print() Function in Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend