Python Directory and File Management

In this Python article, we will discuss the importance and uses of the directory and how to do file management in Python. We will how to create and find a directory, list and rename files and directories, how to remove files and directories, etc. along with the examples. So let’s get started.

1. What is Directory in Python?

In python, we may have huge numbers of files that we have to handle while working with a program. The major part comes here to manage and arrange them in one place so to make the work easier.

We can do it with the directory. The directory can handle a large number of files and if we store our code inside different directories then it will be easy to manage them.

To be specific & simple, a group of files and subdirectories can be termed as a directory or folder. In python, we have the os module which includes many useful methods to work with directories & files.

Read more: Modules in Python


2. How can we create a Directory in Python?

In python, the os module has varieties of functions to interact with the operating system of the user. os is present in python’s standard utility modules.

We can use this module as a portable way of working with the operating system-dependent functionality. The os and os.path modules include many functions to interact with the file system.

In the case, when we do not have a valid or accessible file name or path then all functions in the os module will raise OSError, the error will also be raised when arguments have the correct type but are not accepted by the operating system.

There are different methods available in the OS module for creating a directory. We will discuss the two of them.

  • os.mkdir()
  • os.makedirs()

Both of them does not return any value.

2.1. Create directory using os.mkdir()

We can use the os.mkdir() method to create a directory named along with its path and with the specified numeric mode. This method will raise FileExistsError for a pre-existing directory.

Syntax:
os.mkdir(path, mode = 0o777, *, dir_fd = None)

Parameters

  • path: It represents a file system path. The path-like object can be either a string or bytes object representing a path.
  • mode (optional): An numeric value, represents the mode of the directory to be created. The default value is Oo777 which is an octal number.
  • dir_fd (optional): A file descriptor, refers to a directory. The default value of this parameter is None. If the specified path is perfect then we ignore the dir_fd.
  • *’ in the parameter indicates that all the following parameters are keyword-only and they can be provided using their name, not as a positional parameter.
# Eample to explain os.mkdir() method

import os
directory = "codingeek"
parent_dir = "C:/Users/ASUS/Documents/"
path = os.path.join(parent_dir, directory)
  
# Create the directory
os.mkdir(path)
print("Successfully created '% s' directory" % directory)
  
directory = "Coder"
parent_dir = "C:/Users/ASUS/Documents/"
mode = 0o456
path = os.path.join(parent_dir, directory)
  
# Create the directory with mode 0o456
os.mkdir(path, mode)
print("Successfully created '% s' directory" % directory)
Output
Successfully created 'codingeek' directory
Successfully created 'Coder' directory

Now let’s check what happens when we try to recreate a directory that already exists.

import os 
directory = "codingeek" 
parent_dir = "C:/Users/ASUS/Documents/"
path = os.path.join(parent_dir, directory) 
    
# Create the directory 
os.mkdir(path) 
print("Directory '% s' created" % directory) 
    
# 'FileExistsError' will be raised if directory / file exists
    
# 'FileNotFoundError' will be raised if the specified path 
# is invalid
Output
Traceback (most recent call last):
  File "C:/Users/ASUS/Documents/directory.py", line 7, in 
    os.mkdir(path)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:/Users/ASUS/Documents/codingeek'

To handle such an error we can use the exception handling concept. Let’s implement exception handling for such cases.

import os 
path = "C:/Users/ASUS/Documents/codingeek"
try: 
    os.mkdir(path) 
except OSError as negative: 
    print(negative)  
Output
[WinError 183] Cannot create a file when that file already exists: 'C:/Users/ASUS/Documents/codingeek'

2.2. Create directory using os.makedirs()

We use the os.makedirs() method to create a directory by a recursion. It will make a leaf directory if any intermediate-level directory is missing. We can create a directory tree with help of this method.

"C:/Users/ASUS/Documents/codingeek/author/Garvit"

If the user wants to create a directory ‘Garvit’ but in the path, the directory ‘codingeek’ and ‘author’ is not present, then the os.makedirs() method will create all unavailable/missing directories in the given path.

Syntax:
os.makedirs(path, mode = 0o777, exist_ok = False)

Parameters

  • path: It represents a file system path. A path-like object is either a string or bytes.
  • mode (optional): It is a numeric value that represents the mode of the newly-created directory. The default value is Oo777 which is an octal number.
  • exist_ok (optional):  If the target directory is already in existence giving False value then an OSError is raised else not. The default value is False.
# Example to explain os.makedirs() method  
import os 
directory = "Garvit"      # Leaf directory 
parent_dir = "C:/Users/ASUS/Documents/codingeek/author"
path = os.path.join(parent_dir, directory) 

os.makedirs(path) 
print("Successfully created '% s' Directory" % directory) 
    
# Directory 'codingeek' & 'author' will be created too  
# if they do not exist 
    
directory = "val"    
parent_dir = "C:/Users/ASUS/Documents/codingeek/value"
mode = 0o644    
path = os.path.join(parent_dir, directory)     

os.makedirs(path, mode) 
print("Successfully created '% s' Directory" % directory) 
       
# If any intermediate directory is missing  
# os.makedirs() method will create them 
Output
Successfully created 'Garvit' Directory
Successfully created 'val' Directory

If we try to recreate an already existing directory then it will also throw FileExistsError. Now let’s check for the error when we try to create an existing directory.

import os 
directory = "Garvit"
parent_dir = "C:/Users/ASUS/Documents/codingeek/author"
path = os.path.join(parent_dir, directory) 

os.makedirs(path) 
print("Successfully created '% s' Directory" % directory) 
Output
Traceback (most recent call last):
  File "C:/Users/ASUS/Documents/directory.py", line 5, in 
    os.makedirs(path)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\os.py", line 223, in makedirs
    mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:/Users/ASUS/Documents/codingeek/author\Garvit'

Again let’s try to handle these errors using the exception handling concept. Let’s implement exception handling for such cases.

# importing os module
import os
  
# an OSError will be raised for creating an already existing
# directory. But It can be removed by setting the exist_ok as True

directory = "Garvit"
parent_dir = "C:/Users/ASUS/Documents/codingeek/author"
path = os.path.join(parent_dir, directory)
try:
  os.makedirs(path, exist_ok = True)
  print("Successfully created '% s' Directory" % directory) 
except OSError as error:
  print("Task Unsuccessful")
Output
Successfully created 'Garvit' Directory

3. How to find a current Directory in Python?

In python, we can find the location of the present working directory with the help of the getcwd() method of the os module. It returns a string.

To get the byes objects we can use the getcwdb() method.

import os
print(os.getcwd())
print(os.getcwdb())
Output
C:\Users\ASUS\Documents
Warning (from warnings module):
  File "C:/Users/ASUS/Documents/directory.py", line 3
    print(os.getcwdb())
DeprecationWarning: The Windows bytes API has been deprecated, use Unicode filenames instead
 b'C:\Users\ASUS\Documents'

Here we can notice that the getcwdb() meyhod is deprecated and should not be used for any future development. 


4. How to Change a Directory?

In python, we can change a current working directory by using the chdir() method. We must mention the new path as a string.

We can use either the forward-slash / or the backward-slash \ to separate the path elements.

It is preferred to use an escape sequence while using the backward slash.

import os
os.chdir('C:\\ASUS\\Python')
print(os.getcwd())
Output
C:\ASUS\Python

5. How to get the list of all Directories and Files?

In python, we can get all files and sub-directories inside a directory with the help of the listdir() method.

import os
print(os.listdir())
print(os.listdir('D:\\'))
Output
['!qhlogs.doc', 'Battlefield 2', 'binarysearch.py', 'check.py', 'class with constructor.py', 'Coder', 'codingeek', 'desktop.ini', 'dictfake.py', 'directory.py', 'fakeargument.py', 'fakegloballocal.py', 'faketest.py', 'faketest2.py', 'ft.py', 'function argument test.py', 'hackerearthinput.py', 'IISExpress', 'infinite recursion.py', 'listfake.py', 'My Cheat Tables', 'My Games', 'My Music', 'My Pictures', 'My Videos', 'My Web Sites', 'numbertest.py', 'Pillar1.docx', 'pillar2.py', 'Pillar3.docx', 'Pillar5.py', 'setfake.py', 'Visual Studio 2019', 'xter.py', 'year planet.py']
['!qhlogs.doc', '$RECYCLE.BIN', 'Gta San BERLIN', 'Money Hiest', 'Notepad++', 'peaky blinder', 'Quick Heal', 'songs', 'System Volume Information', 'Taanvi goyal industrial report.docx', 'xampp']

6. How can we Rename a Directory or File?

It is simple as it seems. In python, we can use the rename() method to rename any directory or a file.

This method intakes two arguments: first, the old name & second, the new name.

import os
print(os.listdir())
os.rename('result','new_test')
print(os.listdir())
Output
['result']
['new_test']

7. How to Remove a Directory or File?

In python, we can simply remove a file using the remove() method.

We can also use the rmdir() method which is used only to remove an empty directory.

import os
print(os.listdir())
os.remove('new_test.txt')

print(os.listdir())
os.rmdir('getnew')

print(os.listdir())
Output
['new_test.txt', 'getnew']
['getnew']
[]

8. Conclusion

In this article, we have covered everything about directory in python along with various examples.

  • What is a Directory in Python and how can create a directory?
  • Two method os.mkdir() & os.makedirs()
  • How to find a current Directory in Python and also How to Change a Directory?
  • How to get the list of all Directories and Files and How to Rename a Directory or File and How to Remove a Directory or File?

Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index