How to Move Files and Work with Directories in Python

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Python File System Basics

Working with the file system is one of the most common tasks in Python development. This includes creating and deleting files, copying, moving, renaming them, as well as creating directories and checking if they exist. File operations form the foundation of process automation, web development, data analysis, configuration management, and application deployment.

Python provides a rich set of built-in libraries for efficient file system management. The main tools are the os, shutil, and pathlib modules. These libraries cover virtually every use case—from basic operations to advanced tasks.

Moving Files in Python

Using shutil.move()

When you need to move a file from one directory to another, the simplest and most efficient approach is to use the shutil.move() function. It works similarly to the mv command on Linux and Unix systems.

import shutil

shutil.move('source/file.txt', 'destination/file.txt')

The shutil.move() function handles both individual files and entire directories automatically. When moving a file, you can also rename it at the same time. Keep in mind that if a file with the same name already exists in the target folder, it will be overwritten without warning.

Creating Directories During a Move

When moving files, you often need to create new directories. Here's how to handle that:

import os
import shutil

src = 'data/report.txt'
dst_dir = 'archive'
os.makedirs(dst_dir, exist_ok=True)
shutil.move(src, os.path.join(dst_dir, 'report.txt'))

The os.makedirs() function with exist_ok=True creates the entire directory chain if it doesn't exist, and won't raise an error if the directories are already there.

Moving Directories

The shutil.move() function also supports moving entire directories:

shutil.move('old_folder', 'new_location/old_folder')

Copying Files and Creating Links

Basic File Copying

Python offers several functions for copying files, each with different capabilities:

import shutil

# Copy file contents only
shutil.copy('file.txt', 'backup/file.txt')

The copy() function copies only the file's content, not its metadata. For a full copy that preserves all file attributes, use copy2():

shutil.copy2('file.txt', 'backup/file.txt')

Copying Directories

To copy entire directories with all their contents, use the copytree() function:

shutil.copytree('project', 'project_backup')

Creating Hard Links

A hard link is an additional name for an existing file in the file system:

import os

os.link('file.txt', 'linked_file.txt')

A hard link means the same physical file is accessible through multiple paths. Changes to the file's content are reflected in all links. You can check the number of hard links using os.stat().st_nlink.

Managing Directories

Creating Directories

Python provides two main functions for creating directories:

import os

# Create a single directory
os.mkdir('new_folder')

# Create a nested directory structure
os.makedirs('parent/child/grandchild', exist_ok=True)

The mkdir() function creates only one directory and raises a FileExistsError if the directory already exists.

Blogs

Book Recommendations