Python Directories

The os (operating system) module represents operating system dependent functionality. This module is useful to perform some simple operations on directories. Let's assume we are working with the following directory path: F:\py\enum And we are currently in the directory F:\py where we are running our programs. If we want to know the currently working directory, we can use getcwd() method of 'os' module as shown in Program.

Working Directory

CopiedCopy Code

import os 
#get current working directory 
current = os.getcwd() 
print('Current directory=', current)

Create Directory

If we want to create our own directory in the present directory, we can use the mkdir() method. This method can be used as:

os.mkdir('mysub')

This will create mysub in the present directory. Suppose, we write the following statement:

os.mkdir('mysub/mysub2')

This will create mysub2 in the mysub directory.

A Python program to create a sub directory and then sub-sub directory in the current directory.

CopiedCopy Code

import os 
#create a sub directory by the name mysub 
os.mkdir('mysub') 
#create a sub-sub directory by the name mysub2 
os.mkdir('mysub/mysub2')

A Python program to use the makedirs() function to create sub and sub-sub directories.

CopiedCopy Code

import os 
#create sub and sub-sub directories 
os.makedirs('newsub/newsub2')

Change Directory

We can change our current working directory to another existing directory. For this purpose, we can use chdir() method. For example, if we want to change our directory to newsub2 which is in newsub, we can use chdir() method as:

goto = os.chdir('newsub/newsub2')

Here, our assumption is that newsub is in the current directory and newsub contains newsub2. After executing the preceding statement, our current working directory will be newsub2.

A Python program to change to another directory.

CopiedCopy Code

import os 
#change to newsub2 directory 
goto = os.chdir('newsub/newsub2') 
#get current working directory 
current = os.getcwd() 
print('Current directory=', current)

Delete Directory

To remove a directory, we can use rmdir() method of 'os' module. Let's suppose, our current directory is F:\py. In this, we have newsub directory and inside newsub, we have newsub2 directory. To remove newsub directory, we can write:

os.rmdir('newsub')

The rmdir() method will remove newsub since it is found in the current directory. Suppose, we write the following statement:

os.rmdir('newsub2')

This cannot remove newsub2 as it is not in the current directory. newsub2 is inside newsub that is in the current directory. In this case, path should be given properly as:

os.rmdir('newsub/newsub2')

This will remove the newsub2 directory.

A Python program to remove a sub directory that is inside another directory.

CopiedCopy Code

import os 
#to remove newsub2 directory 
os.rmdir('newsub/newsub2')

A Python program to remove all sub directories that is inside directory.

CopiedCopy Code

import os 
#removes all sub folders in a directory
os. removedirs('Naveen/mysub2/New folder')

List Directory

A Python program to display all contents of the current directory.

CopiedCopy Code

import os 
for dirpath, dirnames, filenames in os.walk('.'): 
   print('Current path:', dirpath) 
   print('Directories:', dirnames) 
   print('Files:', filenames) 
   print()