Skip to main content

Python Directory and Files Management with os Module

A directory is a collection of files and subdirectories. A directory inside a directory is known as a subdirectory.

Python os module provides many useful methods to work with directories (and files as well).

How Get the Current Directory in Python

The getcwd() method is used to get the current working directory, returning it as a string.

For example, try this in your machine to print the current working directory:

import os

print(os.getcwd())

How Change Directory in Python

The chdir(new_path) method is used to change the current working directory, where new_path is a string that specifies the new path.

note

Forward-slash / or backward-slash \ can be used to separate the path elements.

For example:

import os

# Change a directory to "/home/newdir"
os.chdir("/home/newdir")

How List Directories and Files in Python

You can list all files and subdirectories inside a directory using the listdir() method.

For instance, it provides a list of files and subdirectories from the current working directory if no path is specified:

import os

# print current working directory
print(os.getcwd())

# list all sub-directories
os.listdir()

Output:

C:\Python33
['DLLs',
'Doc',
'include',
'Lib',
'libs',
'LICENSE.txt',
'NEWS.txt',
'python.exe',
'pythonw.exe',
'README.txt',
'Scripts',
'tcl',
'Tools']

Otherwise, you can specify a path and obtain of subdirectories and files in that path:

import os

os.listdir('G:\\')

Output:

['$RECYCLE.BIN',
'Movies',
'Music',
'Photos',
'Series',
'System Volume Information']

How Make a New Directory in Python

In Python, you can make a new directory using the mkdir() method, specifying the path of the new directory.

note

If the full path is not specified, the new directory is created in the current working directory.

# Create new directory test in current working directory
os.mkdir('test')

# Check new directory
print(os.listdir()) # Print ['test']

How Rename a Directory or a File in Python

The rename() method can rename a directory or a file.

For renaming any directory or file, rename() takes in two arguments:

  • the old name as the first argument
  • the new name as the second argument.

For example:

import os

print(os.listdir())
# Print['my_old_dir']

# rename a directory
os.rename('my_old_dir','my_new_dir')

print(os.listdir() )
# Print ['my_new_dir']
note

Learn more in Python Rename File Chapter.

How Remove a Directory or a File in Python

In Python, you can use the remove() method or the rmdir() method to remove a file or directory.

For example, to delete a file you can use remove():

import os

# delete "myfile.txt" file
os.remove("myfile.txt")

To delete an empty directory, you can use rmdir():

import os

# delete the empty directory "mydir"
os.rmdir("mydir")

To remove a non-empty directory, you can use the rmtree() method inside the shutil module

import shutil

# delete "mydir" directory and all of its contents
shutil.rmtree("mydir")
note

Learn more in Python Delete File Chapter.

os Python module

The os Python module provides a big range of useful methods to manipulate files and directories.