Working with Directories in Python

The OS module in python provides functions for interacting with the operating system. This module contains an interface to many operating system-specific functions to manipulate processes, files, file descriptors, directories and other “low level” features of the OS.

Current Working Directory

The getcwd() returns the path to the current working directory. This is the directory which the OS use to transform a relative file name into an absolute file name.


List Directory Contents

The listdir() function returns the content of a directory.


Create a new Directory/Folder

The mkdir() method creates a new directory. It returns an error if the parent directory does not exist.


The above example create a new directory "Temp" in the current path.

Creating Subdirectories

Deleting an empty Directory/Folder

The rmdir() method will delete an empty directory or folder.


Renaming a directory/folder

The os.rename() method can rename a folder from an old name to a new one.

 (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About