Select Page

Most programs today require the manipulation of files and folders at some point. As a programmer, you are likely to have to create, move, rename files on a regular basis. Whether your program must move files across disks, sorts photos, or organizes a folder, it must move files and folders, and occasionally needs to create or rename them. Python provides all of the tools you need to simply and efficiently perform these operations on any operating system. 

In this article, we’ll review the core concept of a file and then demonstrate how to move and create files in Python on both Windows and Unix (macOS and Linux) systems.

Refresher: What Is a File? 

To move a file in Python a programmer must first understand the concept of a computer file. Here’s a typical file structure:

In simple terms, a file in a computer is a binary structure composed of a header, a body, and a footer. The header tells the program (usually the operating system) that it marks the file’s beginning, the body contains the file’s actual data (for example, a movie or a text file), and the footer specifies the end. A folder is a container for any type and number of files.

The way files and folders are encoded depends on the operating system used and the file structure (e.g. NTFS, exFat), but the principle is similar. When a software program moves a file from a source to a destination, it carefully handles the header, body and footer blocks of data to ensure that nothing gets cut in the middle, which would result in data corruption. 

Python manipulates files using OS-specific functions to perform actions like create, delete, rename, and move. This prevents you from accidentally corrupting files while performing operations. It also simplifies coding, especially if your program is designed for more than one operating system.

How To Manipulate Files With Python

To communicate effectively with the operating system and perform actions on the computer, Python uses different modules. In this article, we’ll be using three of them: the pathlib module and the shutil module and the OS module.

Handling File Paths with the Pathlib Module

Differences between the most common OS bases (Windows NT, and Unix for Mac and Linux) complicate your ability to move files and folders. Windows and Unix systems have different approaches to paths. Check out the differences between the two file paths below—one is on Windows and the other one is on Unix.


Here’s a Windows path:

C:\Windows\System32\config\Journal

Windows file paths contain a drive letter (C:) and backslashes.

And here’s a Unix path (for Mac or Linux):

/usr/share/code/resources/app/licenses

Unix file paths contain forward slashes and do not contain drive letters.

Given the differences between these paths, it’s best to avoid string operations whenever possible. This helps to ensure that your program works flawlessly on any operating system, since writing file paths manually is likely to result in errors. This is where pathlib comes in handy.

Pathlib uses objects and variables to store paths that are specific, for instance, to a system or to a user. For example, Path.home refers to the home folder of the current user. Whether this user is on a Linux, Windows, or MacOS computer does not matter. Therefore, you can use pathlib’s functions regardless of the operating system.

Let’s now write a program that returns the home directory:

from pathlib import Path
print(Path.home())

This program first imports the Path class from the pathlib module. Next it prints the home path. As the home directories depend on our operating system, the program will give an output based on the OS:

On Windows:

C:\Users\doug

On Linux or Mac:

/home/doug

For example, if your program needs to place a file into the user home folder, path.home guarantees a successful operation.

Copying and Moving Files

In addition to the pathlib module, we’ll be using two other useful modules: the shutil module and the os module. They both allow us to perform operations on files. Shutil works on top of the os module, which is a low-level file manipulation module for Python. Let’s have a look at a program that moves a file from the home directory to the current working directory from which the program runs: 

First, we import the modules and set some variables:

from pathlib import Path
import shutil, os

# A home directory variable using pathlib
homeDir = Path.home()

# The source is a file in the home directory
source = os.path.join(homeDir,"TestDoc.txt")...

The os.path.join function from the os module lets us add the file name to the home directory to create the source. The second part of this code creates the destination directory and moves the file across to the current directory.

...# Destination path is the current path
destination = Path.cwd()

# Move the content of
# source to destination
dest = shutil.move(source, destination)

Path.cwd() picks up the current directory, regardless of the OS, and the shutil.move function uses the source and destination parameters to perform the moving action. If the console does not give any output when running this Python script, it means that the program did not throw any error or exception, and has executed as intended.

Creating and Renaming Files

Python also allows you to create and rename files, using some of the tools we’ve learned about. Let’s say you want to check your current directory for a file, and create one if it isn’t there. Here’s how you would do it:

from pathlib import Path
import os

# Check if a file exists in the current directory

if os.path.isfile('TestDoc.txt') is True:
    print("The file already exists. Exiting.")...

First, we use another os module function called os.path.isfile to check if the document exists. This function will return either “True” or “False.” We use an if statement to adapt the program’s behavior. If the file exists, we do nothing except print a message. 

else: # If the file is not found
   
    # Get the current path
destination = Path.cwd()

    # add the file name to the path
fileName = os.path.join(destination,"TestDoc.txt")
   
# Create the file using the built-in open function
fp =  open(fileName,'x')
print("The file has been created. Exiting.")

Similarly to our first example, we then get the current path, use it to create a complete path to our file, and then use the built-in open function to create the file. The x parameter specifies that we’re only creating the file and not writing in it.

Getting the Most Out of Python 

In this article, we used Python to perform basic file manipulations. We covered some of  the most useful modules, including os, shutil, and pathlib, as they apply to moving and creating files.

Want to dig further into Python?

Udacity’s Introduction to Programming Nanodegree will take you through real-life scenarios as you master the fundamentals of Python, CSS and Javascript. You can count on our mentors and student community to help you along the way.

Enroll in Udacity’s Introduction to Programming Nanodegree today!

Complete Code Examples

Example 1: Move a file

from pathlib import Path
import shutil, os

# A home directory variable using pathlib
homeDir = Path.home()

# The source is a file in the home directory
source = os.path.join(homeDir,"TestDoc.txt")
   
# Destination path is the current path
destination = Path.cwd()

# Move the content of
# source to destination
dest = shutil.move(source, destination)

Example 2: Create a file

from pathlib import Path
import os

# Check if a file exists in the current directory

if os.path.isfile('TestDoc.txt') is True:
    print("The file already exists. Exiting.")

else: # If the file is not found
   
    # Get the current path
destination = Path.cwd()

    # add the file name to the path
fileName = os.path.join(destination,"TestDoc.txt")
   
# Create the file using the built-in open function
fp =  open(fileName,'x')
print("The file has been created. Exiting.")