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

os.mkdir(“new_directory_name”)

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
os.mkdir(‘datafiles’)

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:

os.chdir(‘newdirectory’)

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
os.mkdir(‘datafiles/newfiles’);
os.chdir(‘datafiles/newfiles’);
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( ))
os.chdir(‘datafiles’)
print(os.getcwd())
os.chdir(‘newfiles’)
print(os.getcwd())

Output

D:\pyprogs
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:

os.rmdirQdirectoryname’)

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
os.chdir(‘datafiles’)
os.rmdir(‘newfiles)

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

print(os.listdir())

Output

[‘break_for.py’, 1>reak_while.py’, ‘chdir.py’, ‘check.py’, ‘compileerror.py’, ‘constantly’, ‘continue.py’, ‘customexception.py’, ‘datafiles’, ‘exceptionl.py’, ‘factpy’, ‘factorial.py’, ‘fibonacci.py’, ‘fileattri.py’, ‘for_read_file.py’, ‘fimc.py’, ‘fimc_default_arg.py’, ‘fimc_keyword_arg.py’, ‘func_noarg.py’, ‘func_noargl.py’, ‘fimc_product.py’, ‘fimc_reargl.py’, ‘fimc_reargl_retum.py’, ‘ftmc_var_length.py’, ‘f_series.py’, ‘getcwd.py’, ‘lambda.py’, ‘listdir.py’, ‘local_global.py’, ‘math_func.py’, ‘mkdir.py’, ‘nested_for.py’, ‘output.txt’, ‘pass.py’, ‘pass_by_ref.py’, ‘pass_by_refl.py’, ‘pass_by_val.py’, ‘rand.py’, ‘readlines_file.py’, ‘readline_file.py’, ‘read_file.py’, ‘recur_fact.py’, ‘remove.py’, ‘rename.py’, ‘rmdir.py’, ‘runtime_error.py’, ‘series.py’, ‘tell seek file.py’, ‘test.py’, ‘trigono.py’, ‘userexception.py’, ‘write file.py’, ‘ pycache_’]

Python Tutorial

Leave a Reply

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