Python Directory management

In this article, We are going to learn about files and directory management in python. Different methods available in python os path module. We are going to cover topics like, How to create a folder using python, How to rename a directory using python, Listing all directories and working with them.

What is directory/folder management in python:

Python folder/directory management includes;
- Create new folder/directory.
- Rename existing folder.
- List content of folder.
- Delete a folder.
- Get the current working directory.
- Change current working directory.
Python folder/Directory management.

Basically Directory or folder is a container for holding the files and sub-directories. In context of python directory/folder and file management we are going to perform following operations on directories.

  • Create new directory/folder using python.
  • Rename existing folder/directory or file.
  • List all the directories and files.
  • Removing a directory of file using python.
  • Find current working directory.
  • Changing current working directory.

Python OS module:

In python there is built-in module named “os” is available to deal with Local directories, files, processes, and environment variables. To perform file and directory/folder handling operations in python we can use this “os” module. So let’s get started with each file handling operation.

How to create new directory/folder using python:

Python mkdir() method is available in os module for creating a new folder/directory. This method requires directory or folder name as an input and it will create a new folder/directory at current working directory.

If you want to create a file/directory at a specific location then provide directory name along with its full path.

#Python program to create a new folder/directory.

1. Directory named "New_Folder" is created at C:\Temp location.
2. Directory named "New_Folder" is created at current working directory.

How to rename existing folder/directory or file using python:

rename() method is available in os module for renaming existing files or folders in python. Arguments required to this method are old folder/file name and new folder/file name.

#Python program to rename existing folder/directory.
os.rename('C:\\Temp\\New_Folder', "C:\\Temp\\Renamed")

python rename() method will rename the existing "New_Folder" directory to "Renamed"

How to delete empty folder/directory using python.

rmdir() is the method available in python os module to remove or delete empty directories. This method requires path of folder which we want to remove as an argument. This method only works for the empty directories and folders.

To remove folders/directory with content in it. Please go through the following article of “How to remove files and folders using python”.


#Python program for deleting empty folder/directory using python.

Above program will delete the empty folder (New_Folder).

How to find current working directory using python.

getcwd() is the method available in python to get the current working directory. It returns us the current working directory path in string format.

#Python program to find out current working directory.

#It will print the current working directory path.

How to change current working directory using python.

chdir() method available in python os module for changing the current working directory. This method requires new directory path as an argument.

#Python program for creating new directory and setting it as current working directory.
import os

#New directory named (New_Fold) is created at specified path and it is set as working directory.

How to list all the content inside folder/Directory using python.

listdir() method is available in python os module to list all the folder content. It requires path of the folder of which we want to check the content as an argument.

#Python program to list all the folder content.

Here it will print all the folders and files present inside folder "New".

Continue Learning:

Also you can get more help on python forum.  Click Here

Leave a Comment