Python

P
PythonHub
Progress 0%

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple Python Scripts
    • Namespace in Python
    • 📝 Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • 📝 Assignments
  • Input Output in Python
    • Accept Input
    • Output Formatting
    • 📝 Assignments
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • Short Hand IF-ELSE
    • 📝 Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • 📝 While Loop Assignments
    • For Loop
    • For Loop Examples
    • Nested For Loop
    • Nested For Loop Examples
    • Infinite While Loops
    • Infinite For Loops
    • Break, Continue and Else in Loops
    • Difference Between For and While Loop
    • For Each Loop
    • 📝 For Each Assignments
    • 📝 All Loops Assignments
  • List
    • List in Python
    • Access List Elements
    • List Functions
    • Iterate (Loop) List
    • List Comprehension
    • 📝 Assignments
  • Tuple
    • Tuple in Python
    • Access Tuple Elements
    • Tuple Functions
    • Iterate (Loop) Tuple
    • Unpack Tuple
    • Tuple Comprehension
    • 📝 Assignments
  • Set
    • Set in Python
    • Access Set Elements
    • Set Methods
    • Iterate (Loop) Set
    • Pack/Unpack Set
    • Set Comprehension
    • 📝 Assignments
  • Dictionary
    • Dictionary
    • Access Dictionary Items
    • Dictionary Methods
    • Iterate (Loop) Dictionary
    • Formatting Dictionaries
    • Nested Dictionaries
    • Dictionary Comprehension
    • 📝 Assignments
  • Diff List Tuple Set Dictionary
    • List vs Tuple
    • List vs Set
    • List vs Dictionary
    • Tuple vs Set
    • Tuple vs Dictionary
    • Dictionary vs Set
    • 📝 Assignments
  • Exception
    • Error vs Exception
    • Types of Exception
    • Exception Handling
    • User Defined Exception
    • Logging Exception
    • 📝 Assignments
  • Functions
    • Introduction to Functions
    • Modular Programming
    • Types of Functions
    • Inbuilt Functions
    • Need For User-Defined Function
    • Elements of User Defined Function
    • Function Arguments
    • Nesting of Functions
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • 📝 Assignments
  • Python Module
    • Introduction to Module
    • Inbuilt Modules in Python
    • User Defined Module
    • 📝 Assignments
  • File Handling
    • Introduction to Files
    • Create File
    • Read Files
    • Write to File
    • Rename File
    • Copy File
    • Move File
    • List Files in Directory
    • Binary Files
    • Zipping and Unzipping Files
    • 📝 Assignments
  • Strings
    • Basics of Strings
    • String Special Operators
    • String Formatting Operators
    • String Methods
    • 📝 Assignments
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • OOP vs Procedural Programming
    • Difference Between Classes and Objects
    • Constructors
    • Destructor
    • Built Class Methods and Attributes
    • Class and Instance Variables
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in Python
    • Abstract Class vs Interface
    • Public, Private and Protected
    • Overloading vs Overriding
    • Inheritance vs Composition
    • Encapsulation
    • Polymorphism
    • Inner Classes
    • 📝 Assignments
  • Advanced Python
    • Iterator in Python
    • Generator in Python
    • Decorator in Python
    • Type Hints in Python
    • Async/Await Programming
    • Dataclasses in Python
    • Context Managers in Python
    • Property Decorator in Python
    • __slots__ in Python
    • Enums in Python
    • Walrus Operator in Python
    • Match-Case in Python
    • 📝 Assignments
  • Python Standard Library
    • Collections Module
    • Itertools Module
    • Functools Module
    • Datetime Module
    • JSON Module
    • OS Module
    • Sys Module
    • Random Module
    • Math Module
    • 📝 Assignments
  • Python Testing
    • Unit Testing in Python
    • Pytest Framework
    • Mocking in Python
    • 📝 Assignments
  • Python Best Practices
    • PEP 8 Style Guide
    • Docstrings in Python
    • Logging in Python
    • Code Optimization Tips
    • Debugging Techniques
    • 📝 Assignments
  • MySQL Database in Python
    • Introduction to MySQL with Python
    • DBMS vs File System
    • Connecting to MySQL Database
    • Create Database in MySQL
    • Create Table in MySQL
    • Insert Data in MySQL
    • Insert Multiple Rows
    • Select Data from MySQL
    • WHERE Clause in MySQL
    • Update Data in MySQL
    • Delete Data from MySQL
    • Parameterized Queries
    • Transaction Management
    • Error Handling
    • Connection Pooling
    • MySQL Drivers Guide
    • Joins in MySQL
    • Aggregation Functions
    • Backup MySQL Database
    • Best Practices
    • 📝 Assignments
  • MySQL Database Operations
    • SELECT Statement
    • MySQL Operators
    • DDL Statements
    • DML Statements
    • Subqueries
    • JOIN Operations
    • Aggregation
    • Case Study
    • 📝 Assignments
  • Graphics in Python
  • Threads in Python
    • Introduction to Threads
    • Process vs Threads
    • Concurrent Programming & GIL
    • Uses of Threads
    • Creating Threads
    • Single Tasking
    • Multi Tasking
    • Thread Synchronization
    • 📝 Assignments
  • Interview Questions & Answers
  • Python Case Studies
  • Multiple Choice Questions
  • 📝 Practice Problems
Get in Touch
  • tech2dsm@gmail.com

© Sankalan Data Tech

Python Language Interactive Tutorial

Python: Move File

Python Move File - Complete Guide

Learn how to move files in Python using the shutil module.

Created by Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
Created by experienced Python developers, data engineers, and data scientists to make programming easy through practical examples, real-world experience, and clear explanations.
On this page:
  • Moving Files in Python
  • Using shutil.move()
  • Move vs Rename
  • Moving Multiple Files
  • Moving Directories
  • Error Handling
  • Best Practices
  • Try It Yourself
  • Quiz
  • FAQ
Share this tutorial:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link
What You'll Learn Here
  • shutil.move() — moving files and directories
  • Move vs Rename — understanding the difference
  • Batch moving — moving multiple files
  • Moving directories — relocating entire folders
  • Error handling — robust file moving
  • Best practices — safe file relocation

Moving Files in Python

Moving files is a common operation when you need to organize files, archive data, or restructure your file system. Python's shutil.move() function provides a simple way to move files and directories from one location to another.

Think of moving a file like taking a file from one folder and placing it in another. The file ends up in the new location and is no longer in the original place. It's different from copying, where the original stays put.

💡 Key concept: Moving a file is different from copying it. When you move a file, it is removed from its original location and appears only in the new location. The shutil.move() function handles this in a single operation.

Using shutil.move()

1

The Primary Way to Move Files

# The shutil.move() function moves a file or directory

import shutil
import os

# Create a sample file
with open("file_to_move.txt", "w") as f:
    f.write("This file will be moved.\n")
    f.write("It will no longer be in the original location.\n")

print("Created: file_to_move.txt")

# 1. Basic move
shutil.move("file_to_move.txt", "moved_file.txt")
print("Moved: file_to_move.txt → moved_file.txt")

# 2. Move to a different directory
os.makedirs("target_folder", exist_ok=True)
shutil.move("moved_file.txt", "target_folder/moved_file.txt")
print("Moved: moved_file.txt → target_folder/moved_file.txt")

# 3. Move with a new name
shutil.move("target_folder/moved_file.txt", "target_folder/renamed_file.txt")
print("Moved and renamed: target_folder/moved_file.txt → target_folder/renamed_file.txt")

# 4. Check that the original no longer exists
if not os.path.exists("moved_file.txt"):
    print("The original file is gone (as expected)")

# 5. Verify the file is in the new location
if os.path.exists("target_folder/renamed_file.txt"):
    print("File is in the new location")

shutil.move() key points:

  • Removes original — file no longer exists at source
  • Can rename — change name while moving
  • Works on directories — can move entire folders
  • Overwrites by default — if destination exists
  • Cross-platform — works on all operating systems

Quick Check: What happens to the original file when you use shutil.move()? (Answer: It is removed from the original location)

Move vs Rename

2

Understanding the Difference

# Move and rename are similar but different operations

import shutil
import os

# Create a test file
with open("test_file.txt", "w") as f:
    f.write("Testing move vs rename\n")

print("Created: test_file.txt")

# 1. Rename using os.rename() (same directory)
os.rename("test_file.txt", "renamed_file.txt")
print("Renamed: test_file.txt → renamed_file.txt")

# 2. Move using shutil.move() (different directory)
os.makedirs("archive", exist_ok=True)
shutil.move("renamed_file.txt", "archive/archived_file.txt")
print("Moved: renamed_file.txt → archive/archived_file.txt")

# 3. Move and rename in one operation
shutil.move("archive/archived_file.txt", "archive/final_file.txt")
print("Moved and renamed: archive/archived_file.txt → archive/final_file.txt")

# Compare:
# os.rename() - works within the same filesystem
# shutil.move() - works across filesystems and can move directories

# 4. When to use each
# Use os.rename() when:
# - You're just changing the name in the same directory
# - You need to rename directories

# Use shutil.move() when:
# - You're moving between different directories
# - You want to move and rename in one operation
# - You need to move across different filesystems

Move vs Rename comparison:

  • os.rename() — simple rename within same directory
  • shutil.move() — move to different location, can also rename
  • os.rename() is faster — for simple renames
  • shutil.move() is more powerful — works across filesystems

Quick Check: When should you use shutil.move() instead of os.rename()? (Answer: When moving between different directories or filesystems)

Moving Multiple Files

3

Organizing Files in Bulk

import shutil
import os

# Create sample files
for i in range(3):
    with open(f"doc_{i}.txt", "w") as f:
        f.write(f"Document {i}\n")

print("Created: doc_0.txt, doc_1.txt, doc_2.txt")

# 1. Move all files matching a pattern
def move_matching_files(source_dir, dest_dir, pattern):
    """Move files matching a pattern to a destination"""
    os.makedirs(dest_dir, exist_ok=True)
    
    for filename in os.listdir(source_dir):
        if pattern in filename and os.path.isfile(os.path.join(source_dir, filename)):
            source_path = os.path.join(source_dir, filename)
            dest_path = os.path.join(dest_dir, filename)
            shutil.move(source_path, dest_path)
            print(f"Moved: {filename} → {dest_dir}/")

move_matching_files(".", "documents", "doc_")

# 2. Move files based on extension
def move_by_extension(source_dir, dest_dir, extension):
    """Move files with a specific extension"""
    os.makedirs(dest_dir, exist_ok=True)
    
    for filename in os.listdir(source_dir):
        if filename.endswith(extension):
            source_path = os.path.join(source_dir, filename)
            dest_path = os.path.join(dest_dir, filename)
            shutil.move(source_path, dest_path)
            print(f"Moved: {filename} → {dest_dir}/")

move_by_extension(".", "text_files", ".txt")

# 3. Move files with progress tracking
def move_with_progress(source_dir, dest_dir):
    """Move files with progress indication"""
    files = [f for f in os.listdir(source_dir) if os.path.isfile(os.path.join(source_dir, f))]
    total = len(files)
    
    os.makedirs(dest_dir, exist_ok=True)
    
    for i, filename in enumerate(files, 1):
        source_path = os.path.join(source_dir, filename)
        dest_path = os.path.join(dest_dir, filename)
        shutil.move(source_path, dest_path)
        print(f"Progress: {i}/{total} - Moved: {filename}")

# 4. Move with filtering
def move_filtered_files(source_dir, dest_dir, filter_func):
    """Move files that pass a filter condition"""
    os.makedirs(dest_dir, exist_ok=True)
    
    for filename in os.listdir(source_dir):
        source_path = os.path.join(source_dir, filename)
        if os.path.isfile(source_path) and filter_func(filename):
            dest_path = os.path.join(dest_dir, filename)
            shutil.move(source_path, dest_path)
            print(f"Moved: {filename} → {dest_dir}/")

Batch moving strategies:

  • Pattern matching — move files with specific names
  • Extension based — move files by file type
  • Progress tracking — monitor move operations
  • Filtering — use custom conditions

Quick Check: How can you move only files with a specific extension? (Answer: Check filename.endswith(extension))

Moving Directories

4

Relocating Entire Folders

import shutil
import os

# Create a directory structure
os.makedirs("source_folder/subfolder", exist_ok=True)

with open("source_folder/file1.txt", "w") as f:
    f.write("File 1 in source_folder\n")

with open("source_folder/subfolder/file2.txt", "w") as f:
    f.write("File 2 in subfolder\n")

print("Created: source_folder/ with files")

# 1. Move an entire directory
shutil.move("source_folder", "destination_folder")
print("Moved: source_folder → destination_folder")

# 2. Verify the directory moved
if os.path.exists("destination_folder"):
    print("Directory moved successfully")
    print("Contents:", os.listdir("destination_folder"))

# 3. Move directory with overwrite protection
def safe_move_dir(source, destination):
    """Move directory safely with checks"""
    if not os.path.exists(source):
        print(f"Source '{source}' does not exist")
        return False
    
    if os.path.exists(destination):
        print(f"Destination '{destination}' already exists")
        response = input("Overwrite? (y/n): ")
        if response.lower() != 'y':
            print("Move cancelled")
            return False
    
    shutil.move(source, destination)
    print(f"Moved: {source} → {destination}")
    return True

# 4. Move directory and verify
def move_and_verify(source, destination):
    """Move directory and verify it worked"""
    try:
        shutil.move(source, destination)
        
        if not os.path.exists(source):
            print(f"Source '{source}' no longer exists")
        if os.path.exists(destination):
            print(f"Destination '{destination}' exists")
            return True
        return False
    except Exception as e:
        print(f"Error: {e}")
        return False

Directory moving key points:

  • Moves all contents — entire directory and subdirectories
  • Removes source — original directory is gone
  • Can move across drives — works across filesystems
  • Handle conflicts — destination may already exist

Quick Check: Does shutil.move() work on directories? (Answer: Yes, it moves entire directories)

Error Handling When Moving

5

Robust File Moving

import shutil
import os

# 1. Comprehensive move function
def safe_move(source, destination):
    """Safely move a file or directory with error handling"""
    try:
        # Check if source exists
        if not os.path.exists(source):
            print(f"Error: Source '{source}' does not exist")
            return False
        
        # Check if destination exists
        if os.path.exists(destination):
            print(f"Warning: Destination '{destination}' already exists")
            response = input("Overwrite? (y/n): ")
            if response.lower() != 'y':
                print("Move cancelled")
                return False
        
        # Perform the move
        shutil.move(source, destination)
        print(f"Moved: {source} → {destination}")
        return True
    
    except PermissionError:
        print(f"Error: Permission denied when moving '{source}'")
        return False
    
    except OSError as e:
        print(f"Error: {e}")
        return False
    
    except Exception as e:
        print(f"Unexpected error: {e}")
        return False

# 2. Move with automatic backup
def move_with_backup(source, destination):
    """Move with automatic backup of existing destination"""
    if os.path.exists(destination):
        backup = destination + ".backup"
        shutil.move(destination, backup)
        print(f"Existing destination backed up as: {backup}")
    
    return safe_move(source, destination)

# 3. Move with verification
def move_and_verify(source, destination):
    """Move and verify the operation was successful"""
    if safe_move(source, destination):
        if not os.path.exists(source) and os.path.exists(destination):
            print("Move verified successfully")
            return True
        else:
            print("Move verification failed")
            return False
    return False

Common move errors:

  • FileNotFoundError — source doesn't exist
  • PermissionError — can't access source or destination
  • OSError — other operating system issues
  • Always handle errors — make your code robust

Quick Check: What error occurs when you try to move a file that doesn't exist? (Answer: FileNotFoundError)

Best Practices for Moving Files

6

Professional File Moving Guidelines

import shutil
import os

# 1. Always check if source exists
def smart_move(source, destination):
    """Move with existence checks"""
    if not os.path.exists(source):
        print(f"Cannot move: '{source}' does not exist")
        return False
    
    try:
        shutil.move(source, destination)
        print(f"Moved: {source} → {destination}")
        return True
    except Exception as e:
        print(f"Error: {e}")
        return False

# 2. Create destination directory if needed
def move_with_dirs(source, destination):
    """Move and create destination directory if needed"""
    dest_dir = os.path.dirname(destination)
    if dest_dir and not os.path.exists(dest_dir):
        os.makedirs(dest_dir, exist_ok=True)
        print(f"Created directory: {dest_dir}")
    
    return smart_move(source, destination)

# 3. Use the right tool for the job
# For moving between directories: shutil.move()
# For simple renaming: os.rename()
# For copying: shutil.copy()

# 4. Handle conflicts gracefully
def move_with_conflict_handling(source, destination):
    """Move with conflict resolution options"""
    if os.path.exists(destination):
        print(f"Destination '{destination}' exists")
        print("Options:")
        print("  [o] Overwrite")
        print("  [b] Backup existing")
        print("  [s] Skip this file")
        print("  [c] Cancel all")
        
        choice = input("Choose option: ").lower()
        
        if choice == 'o':
            return smart_move(source, destination)
        elif choice == 'b':
            backup = destination + ".backup"
            shutil.move(destination, backup)
            print(f"Backed up: {destination} → {backup}")
            return smart_move(source, destination)
        elif choice == 's':
            print(f"Skipped: {source}")
            return False
        else:
            print("Cancelled")
            return False
    
    return smart_move(source, destination)

# 5. Use meaningful destination names
# ✅ Good
# archive/report_2026_08.csv
# processed/data_final.txt

# ❌ Bad
# temp/file.txt
# new/file.txt

Best practices summary:

  • Check source — verify file/directory exists
  • Create directories — ensure destination exists
  • Handle conflicts — deal with existing files
  • Use right tool — move vs rename vs copy
  • Meaningful names — use clear naming conventions

Quick Check: What is the most important check before moving a file? (Answer: Verify the source exists)

Try It Yourself

Experiment with moving files in the editor below. Try different move operations and see the results.

Loading Pyodide... 0%
Python Code Editor
========================================
MOVE FILE PRACTICE
========================================

1. CREATING SAMPLE FILES
Created: original.txt

2. BASIC MOVE
Moved: original.txt → moved.txt

3. MOVE TO DIRECTORY
Moved: moved.txt → archive/

4. MOVE AND RENAME
Moved and renamed: archive/moved.txt → archive/final.txt

5. CHECKING RESULTS
Files in archive directory: ['final.txt']
original.txt is gone (as expected)

Move file practice complete!
🏆

You've Got It!

You now know how to move files and directories in Python. You understand the difference between moving and renaming, and how to handle errors professionally.

Quick Quiz

Test what you've learned:

1. What function is used to move a file in Python?
2. What happens to the original file when you move it?
3. What is the difference between shutil.move() and os.rename()?
4. Can shutil.move() move directories?
5. What should you check before moving a file?

Frequently Asked Questions

What is the difference between moving and copying a file? ▼

When you move a file, it is removed from its original location and placed in the new location. When you copy a file, the original stays in place and a duplicate is created at the new location. Moving is like relocating, copying is like duplicating.

Can I use shutil.move() to rename a file? ▼

Yes! When you move a file to a different location with a new name, you're effectively renaming it. For simple renaming in the same directory, os.rename() is usually faster and more direct.

What happens if the destination already exists? ▼

shutil.move() will overwrite the destination if it exists. If you want to avoid overwriting, check for existence using os.path.exists() before moving, or create a backup first.

What's a common interview question about moving files? ▼

Common questions include: "How do you move a file in Python?" "What is the difference between os.rename() and shutil.move()?" and "How would you move all files with a specific extension to a different directory?"

Can I move files between different drives or filesystems? ▼

Yes! shutil.move() can move files across different drives and filesystems. It will copy the file to the new location and then delete the original. os.rename() cannot do this reliably.

How do I move a file safely without losing data? ▼

To move safely, first check if the source exists and the destination doesn't already contain important data. Create a backup before moving, use try-except blocks to handle errors, and verify the move was successful by checking the new location.

Where to Go From Here

Now that you can move files, check out these related topics:

List Files in Directory

Learn how to list and filter files.

Learn More →

Binary Files

Learn how to read and write binary files.

Learn More →

Zipping and Unzipping Files

Learn how to compress and extract files.

Learn More →
Interview Resources
  • Python Syntax & Variables Interview Questions
  • Top SQL Interview Questions & Answers
  • SQL Joins: Displaying Data from Multiple Tables FAQ
Previous: Copy File Next: List Files in Directory →