Python Directory and Files Management



Python Directory

When a Python program grows up, we start working with many files. So to handle those different files, we can arrange our code within different directories to make things more manageable.

In Python, a directory or folder is a collection of files and subdirectories.

Python provides the os module that contains different useful methods to work with directories and files.


Get Current Directory

The getcwd() method of the os module is used to get the present working directory.

The getcwd() method returns the current working directory in the form of a string. To get the current working directory as a bytes object, we can use the getcwdb() method.

import os

print("os.getcwd() :", os.getcwd())
print("os.getcwd() :", os.getcwdb())

Output

os.getcwd() : /home/expectocode
os.getcwd() : b'/home/expectocode'

Changing Directory

The chdir() method of the os module is used to change the current working directory.

The new path we want to change into must be provided as a string to the chdir() method. We can use both the forward-slash / or backward-slash \ to separate the path parts.

When using the backward-slash \, it is safer to use an escape sequence, so the backward-slash \ will be \\.

import os

os.chdir("/home/expectocode/new_folder")
print(os.getcwd())

Output

/home/expectocode/new_folder

List Directories and Files

The listdir() method of the os module is used to retrieve all files and sub-directories inside a directory.

The listdir() method takes in a path and returns a list of subdirectories and files in that path. Using the listdir() method without specifying a path returns the list of subdirectories and files from the current working directory.

import os

print(os.getcwd)
print(os.listdir())
print(os.listdir("/home/expectocode/perso"))

Output

/home/expectocode

['new_folder',
'perso',
'example.txt',
'.config']

['movies',
'music',
'photos',
'series']

Creating a New Directory

The mkdir() method of the os module is used to create a new directory.

The mkdir() method takes in the path of the new directory. If the full path is not specified, the new directory is created in the current working directory.

import os

os.mkdir("demo")
print(os.listdir())

Output

['demo']

Renaming a Directory or a File

The rename() method of the os module is used to rename a directory or a file.

The rename() method takes in two primary arguments: the old name as the first argument and the new name as the second argument.

import os

print(os.listdir())
os.rename("demo", "example")
print(os.listdir())

Output

['demo']
['example']

Removing a Directory or a File

The remove() method of the os module is used to remove (delete) a file.

Similarly, the rmdir() method of the os module is used to remove an empty directory.

import os

print(os.listdir())
print(os.remove("test.txt"))
print(os.listdir())

os.rmdir("demo")
print(os.listdir())

Output

['demo', 'test.txt']
['demo']
[]

Note: the rmdir() method can only remove empty directories.

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

import os
import shutil

print(os.listdir())

shutil.rmtree("new_folder")
print(os.listdir())

Output

['new_folder']
[]


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.