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
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
getcwd( ) method
The getcwd() method is used to obtain the information of the present working directory. The general syntax of getcwd() is given below
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
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
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:
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
[‘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_’]