Python Programming - Directories in Python

Python Programming – Directories in Python

So far, we have discussed the creation of files in Python and various „ operations upon them. However, we can also work on directories in Python. This is achieved by making use of the os module and various methods in it. Here, we discuss all the methods associated with the directories in Python.

mkdir( ) method

The mkdir() method is used to create directories in the current directory. This method contains only one argument that is the name of the directory to be created. The syntax for using the mkdir is given as follows


The programming example of mkdir() is presented in Code 9.15. After the execution of this program, a new directory data file is created in the current folder.

Code: 9.15. Illustration of mkdir( ) method.

# Illustration of creating a directory

Import os

chdir( ) method

The chdir() nlethod is used to change the current directory. It also takes one argument for moving from one directory to another and making it the current directory. The general syntax of chdir is given as follows:


The programming example representing the use of chdir is presented in Code: 9.16. In this code, we see that initially we create a file new directory “newfiles” inside the existing directory “datafiles” by using the mkdir() method. Then, we change the directory control to the newly created Chapter 9: File Management in Python directory “newfiles” by using the chdir() method. Subsequently, we create a new file “input.txt” in write mode and write a message into it. The programmer can see in the parent directory that a new directory “newfiles” and a new file “input.txt” are created.

Code: 9.16. Illustration of chdirQ method.

# illustration of changing a directory

import os
fp=open(‘input.txt’, V)
fp.write(‘Hello, Welcome to Programming in Python’)
fp.close( )

getcwd( ) method

The getcwd() method is used to obtain the information of the present working directory. The general syntax of getcwd() is given below

os.getcwd( )

The programming example of getcwd is given in Code 9.17. In this program, we see that initially, we get the information about the current working directory using the getcwd( ) method. Then, we change the directory to data files, and then by using getcwdO we get the path of the present working directory. Subsequently,.we again change the directory to new files and the new location of the current working directory is printed.

Code: 9.17. Illustration of getcwdQ method.

# illustration of changing a directory

impoft os
print(os.getcwd( ))


D :\pyprogs\datafiles
D :\pyprogs\datafiles\newfiles

rmdir( ) method

The rmdir() method is used to remove or delete the directory, which is mentioned in its argument. The general syntax of rmdir() is given as below:


The programming example of rmdir() method is given in Code 9.18. In this program, we delete the directory, which was created in Code 9.15. For this, first we need to change the directory to datafiles to make it the presently working directory. Then, the method rmdir() is executed to delete the newfiles directory.

Code: 9.18. Illustration ofrmdir( ) method.

# Illustration of changing a directory

import os

listdir( ) method

The listdir() method is used to get the list of all files and directories in the currently working directory. The syntax for listdirO is given as follows:

os.listdir( )

The programming example for listdir() is given in Code 9.19. This program lists all the files present in the currently working directory.

Code: 9.19. Illustration of listdir( ) method.

# Illustration of listdir() method

import os



[‘’, 1>’, ‘’, ‘’, ‘’, ‘constantly’, ‘’, ‘’, ‘datafiles’, ‘’, ‘factpy’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘output.txt’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘tell seek’, ‘’, ‘’, ‘’, ‘write’, ‘ pycache_’]

Python Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *