Python Directory

Summary: in this tutorial, you’ll learn how to manipulate directories using Python built-in functions

Get the current working directory

The directory that the Python program is in is known as the current working directory for that program. In order to get the current working directory you’ll need to use the os module with the function getcwd() as follows:

import os cwd = os.getcwd() print(cwd)
Code language: JavaScript (javascript)

To change the current working directory you can use function chdir(). You just pass the current working directory you want to change to as follows:

import os os.chdir('c:\\temp') cwd = os.getcwd() print(cwd)
Code language: JavaScript (javascript)

Join and split a path

To make a program work across platforms including Windows, Linux, and Unix, you need to use a platform-independent file and directory path. Python provides a submodule os.path that contains several useful functions and constants to join and split paths.

The join() function joins path components together and returns a path with exact platform separator for instance in windows backslash () and Unix (/)

The split() function splits path into path components without path separator. Here’s an example of using join() and split() functions:

import os fp = os.path.join('temp','python') print(fp) # temp\python (in Windows) pc = os.path.split(fp) print(pc) # ('temp', 'python')
Code language: PHP (php)

Test if a path is a directory

In order to check a path exists in a system and a path is a directory you can use the functions exists() and isdir() in the submodule os.path as below:

import os path = os.path.join("C:\\","temp") if os.path.exists(path): print(path + ' : exists') if os.path.isdir(path): print(path + ' : is a directory')
Code language: PHP (php)

Create a directory

To create a new directory you use mkdir() or makedirs() functions of os module. And you should always check if a directory exists first before creating a new directory.

The following example creates a new directory called python under the c:\temp directory.

import os dir = os.path.join("C:\\","temp","python") if not os.path.exists(dir): os.mkdir(dir)
Code language: JavaScript (javascript)

Rename a directory

To rename the directory you use os.rename() function. You need to pass the directory you want to change and the new path.

import os oldpath = os.path.join("C:\\","temp","python") newpath = os.path.join("C:\\","temp","python3") if os.path.exists(oldpath): os.rename(oldpath, newpath) print("'{0}' is renamed to '{1}'".format(oldpath,newpath)) #'C:\temp\python' is renamed to 'C:\temp\python3'
Code language: PHP (php)

Delete a directory

To delete a directory, you use function rmdir() function as follows:

import os dir = os.path.join("C:\\","temp","python") if os.path.exists(dir): os.rmdir(dir) print(dir + ' is removed.')
Code language: JavaScript (javascript)

Traverse a directory recursively

Python provides os.walk() function that allows you to traverse a directory recursively. The os.walk() function returns the root directory, the sub-directories, and files.

The following example shows how to print all files and directories in the c:\temp directory:

import os rootdir = "c:\\temp" for root, dirs, files in os.walk(rootdir): print("{0} has {1} files".format(root, len(files)))
Code language: JavaScript (javascript)

Did you find this tutorial helpful ?