File and Folder Operations using Python's OS Module

An introduction to the os module which is typically used to perform file and folder operations when working in Python.
Andrew Wood  •   07 June 2022
Andrew Wood  •   Last Updated: 07 June 2022
An introduction to the os module which is typically used to perform file and folder operations when working in Python.

Introduction

Python's os module provides a set of functions to allow you to interact with your operating system. The module is a part of Python's standard utility modules which means that it is bundled with any clean Python install. It is most commonly used to interact with your file system; specifying paths, creating and deleting folders, or renaming files. Part of the power of the os module is that for many of its common uses it is operating system independent. This means that you can develop on a Windows PC and deploy to a Linux machine without having to rewrite the sections of code dealing with filepaths (Windows and Linux specify paths differently).

This tutorial will walk through some of the more often used functionality that the os module offers. Let's get started.

In order to use the os module you'll first need to import it. This should be placed along with the rest of your imports at the start of your script.

import os

Current Working Directory

The concept of a current working directory (cwd) in the context of running a python programme is one that tends to confuse many developers who are starting out. It is not a difficult concept to master but in order to understand the working directory you must first be comfortable with the concept of files and directories (folders).

A directory is simply a file, whose contents are a collection of other files. This structures provides a way to organise files and folders in order to create an orderly and logical structure. As the name implies, the current working directory is simply the directory in which the files that you are currently working with are sitting. You can think of the File Explorer window as a current working directory.

Perhaps an even better way to visualise the cwd is to think of a terminal prompt.   

visualisation of the current working directory in a terminal

As you navigate through the file system you are changing the current working directory. If you want to access files within the current working directory you do not need to include the entire path from the root but rather just the name of the file within the current working directory.

This works the same with when using the os module. Every process has a current directory which it inherits from it's parent process. This doesn't have to be the directory in which the program you are running is located.

In Python the current working directory is returned as a string using the os.getcwd() function. You can then change the current working directory with os.chdir().

import os
# show the current working directory
cwd = os.getcwd()
print(cwd)

Output:

"C:\Winpython\WPy64-39100\notebooks\canardanalytics\general-python\os-module"

Now we change the current working directory using the os.chdir() method:

newcwd_str = "C:\\temp_example"
# chdir() works the same as cd in a terminal
os.chdir(newcwd_str)
cwd=os.getcwd()
print(cwd)

Output:

"C:\temp_example"

Accessing Files in the Current Working Directory

All files and folders within the current working directory can be easily accessed by simply referring to their name rather than the absolute or root path to the file or directory.

In our example the current working directory has two files.

 

with open('hello.txt','w') as f: # no need to provide full path
    f.write("I can access this file now.")

os.path Module

The os.path() module is a very powerful way to work with and manipulate paths within your Python application. One of the many reasons to use the module is the fact that it is operating system independent and so will work whether you are working in a Windows environment or a Linux environment.

os.path.join

The os.path.join() function takes two or more string paths and intelligently joins them to form a single filepath that is valid on all operating systems.

import os

os.path.join(path,*paths)
# paths are joined from first input to last)

parentdir = 'folder1'
childir = 'folder2'
filename = 'anewtextfile.txt'
newpath = os.path.join(parentdir,childir,filename)
>>> print(newpath)
'folder1/folder2/anewtextfile.txt'

In the next example let's create a path that will join a folder located in the current working directory to a file within the folder.

# we are going to create a new folder called "myfolder" in the current working directory
# will also create a new file in this folder called "anewtextfile.txt"
newdirectory = "myfolder"
newfile = "anewtextfile.txt"

path_to_file = os.path.join(newdirectory,newfile)

# we can then print the path to the file
print(path_to_file)

When you look at the result you should notice that the file path is given relative to the current working directory.

Output:

"myfolder\anewtextfile.txt"

Full Path with os.path.abspath

If you want the full path to the file then you can use os.path.abspath().

abs_path_to_file = os.path.abspath(path_to_file)
print(abs_path_to_file) # print out the full path

Output:

"C:\temp_example\myfolder\anewtextfile.txt"

Check Whether the Path Exists using os.path.exists

Use os.path.exists() to check whether the specified path exists or not. The result is a boolean, either True or False.

# now check whether the folder exists:
folder_exists = os.path.exists(newdirectory)
print(folder_exists)

Output:

False

Since the folder does not yet exist we can use the os module to create it.

Create, Rename and Delete

Create a New Directory

If the folder/directory does not exist then you can create it using os.makedir()

newdirectory = "myfolder"
newfile = "anewtextfile.txt"

path_to_file = os.path.join(newdirectory,newfile)
folder_exists = os.path.exists(newdirectory)

if not folder_exists:
    os.mkdir(newdirectory)

Rename a Directory

To rename a directory use os.rename().

olddirname = 'myfolder'
# now let's rename 'myfolder' to 'myrenamedfolder'
newdirname = 'myrenamedfolder'
os.rename(olddirname,newdirname)

Delete a File

To delete a file use os.remove()

# now delete 'anewtextfile' inside 'myrenamedfolder'
# syntax: os.remove('path_to_file')
os.remove(os.path.join(newdirname,newfile))

Extracting the File Extension

The file extension can be extracted from a filepath with the os.path.splitext() function. The output is a tuple (root,ext) such that root + extension == path. The extension will either be empty or begin with a single period . and contain at most one period.

newdirname = 'myrenamedfolder'
newfile = 'anewtextfile.txt'

myfilepath = os.path.join(newdirname,newfile)
split_file_path = os.path.splitext(myfilepath)
print(split_file_path)
print(split_file_path[1]) # extract just the extension

Output:

('myrenamedfolder/anewtextfile', '.txt')
'.txt'
Share this
Comments
Canard Analytics Founder. Python development, data nerd, aerospace engineering and general aviation.
Profile picture of andreww
Share Article

Looking for a partner on your next project?

Contact Us