In this article, you will learn about Python directory management, how to create directories, how to rename them and how to work with them.
What is a Python Directory?
A directory simply is a structured list of documents and folders. A directory can have sub-directories and files. When we have too many files, Python directory comes in handy in file management with directories and sub-directories.
os module with multiple methods defined inside for directory and file management.
How to create a new directory in Python?
To create a new directory Python has a function
mkdir( ) built into
name is the directory to be created in the current working location.
>>> import os >>> os.mkdir('abc') #create a directory abc in current directory >>> os.mkdir('C:/abc') #create a direcotry abc in C:
Note: Use the forward slash
/ while creating directories in another location.
How to get the current directory location?
To get the location of the current working directory, Python has a function
getcwd( ) built in
Let’s see how this is done:
>>> import os >>> os.getcwd() #get the current location 'C:\\Python\\examples' >>> print (os.getcwd()) 'C:\Python\examples
How to rename a directory in Python?
rename( ) function to rename a directory.
Here is an example, where we will change the name of the file
>>> import os >>> os.rename('xyz.txt','abc.txt')
How to change directories in Python?
chdir( ) function defined in module
os is used to change the working directories.
Suppose we want to change our working directory to
xyz folder in
D:. Here is how it is done.
>>> import os >>> os.chdir('D:/xyz') #this will change our working directory
How to list directories in Python?
listdir( ) function in module
os to list all the directories and files in a particular location.
listdir( ) returns a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries
'..' even if they are present in the directory.
Here is an example:
>>> import os >>> os.listdir() ['files', 'example.txt', 'try.py', python_logo.png']
The function returned the list of files and directories in our working directory. This output depends upon the directories and files contained within the target directory.
How to remove or delete a directory in Python?
To remove or delete a directory path in Python,
rmdir( ) is used which is defined in os module.
rmdir( ) works only when the directory we want to delete is empty, else it raises an OS error.
So here are the ways to remove or delete empty and non-empty directory paths.
Removing empty directories
>>> import os >>> os.listdir() ['example.txt'] >>> os.rmdir('example.txt') >>> os.listdir() 
Removing non-empty directories
rmdir( ) works only when a directory is empty. If we have to remove or delete the directory path of non-empty directory, we have to use
retree( ) funtion defined in
>>> import os >>> import shutil >>> os.listdir() ['xyz'] >>> shutil.rmtree('xxyz') >>> os.listdir() 
Removing multiple directories at once
To remove multiple directories at once, Python has function
removedirs( ) defined in
removedirs( ) remove all the parent directories mentioned in the directory path recursively until an exception is raised.
Let’s take a deeper look with an example:
Suppose we have a directory
y, which is child directory of
Now to remove all three directories at once we can use
removedirs( ) in following way.
>>> import os >>> os.removedirs('x/y/z')
This will delete the directory path of all
z if no exception is raised.
First it will remove
z, then it will remove
z's parent directory
y and finally
y's parent directory
y is non-empty and hence raises and exception while removing, only
x will be deleted.
Python Directory methods
Here is the tabular list of Python directory methods defined in os module for directory management.
|Python directory methods with description|
Use the real
Used to change the current working directory to the path specified.
Set the flags of path to the numeric flags.
Change the mode of path to the numeric mode.
|os.chown(path, uid, gid)|
Change the owner and group id of path to the numeric
Changes the root directory of the current process to the specified path.
Changes the current working directory to the directory represented by the file descriptor
It returns a string representing the current working directory.
It returns a Unicode object representing the current working directory.
Set the flags of path to the numeric flags, like
It changes the mode of path to the numeric mode.
|os.lchown(path, uid, gid)|
It changes the owner and group id of path to the numeric
Returns a list containing the names of the entries in the directory given by path.
It works like
It creates directories recursively.
Creates a new directory named path.
Returns a string representing the path to which the symbolic link points.
Remove directories recursively starting from child to parent.
Rename the directory
It renames the old directories with a new name recursively.
Removes the directory path specified.
Perform a stat system call on the given path.