Python Directory


August 23, 2021, Learn eTutorial
1521

In this tutorial you will master everything about python directories, what is the significance of an OS module in python, and how to create, change, rename and remove directories with examples.

What is a python directory?

A directory is a simple folder with a repertoire of files and subdirectories which in turn may contain subdirectories and files. In a computer system directories are used to organize the files in a well-structured manner. To fetch the contents in files or files itself python needs to interact with the operating system, hence python allows the import of the OS module to a certain program.

What is an OS Module?

The OS module in python which comes under the standard utility module enables the interaction with operating systems dependent functionality. The os module provides certain functionalities that help to manipulate the files and directories.

In our previous tutorial, we have learned to import a module. Let's recall first.

import os 

To get the methods in this module use the below syntax :

import os
print(dir(os))

The output will be a list of methods used in the OS module as given below:

['DirEntry', 'F_OK', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_AddedDllDirectory', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_check_methods', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_putenv', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 'add_dll_directory', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 'stat', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'walk', 'write']

How to get the current working directory?

Now if you wish to know the path of the current directory you are working, you can use getcwd() methods.

import os
os.getcwd()
'C:\\Users\\AppData\\Local\\Programs\\Python\\Python38-32'

The getcwd() method returns the path of the current working directory in python as a string as in the example. You can even check the type of outcome using the type() method.

import os
type(os.getcwd())

Pondering the output of getcwd() we can see the use of two black slashes (\\) in the path to separate the path components. The extra backslash acts as the escape character to the following string. We can take out this using the print function and the same path changes to as shown below:

import os
print(os.getcwd())
C:\Users\AppData\Local\Programs\Python\Python38-32

If you want to get the path of the current working directory as a byte object you can just append ‘b’ to getcwd() method, i.e, use getcwdb() method.

import os
os.getcwdb() 
b'C:\\Users\\AppData\\Local\\Programs\\Python\\Python38-32'

How to change a working directory?

To change the current working directory we can use the built-in method chdir(). The new path should be given as the argument. Both backslash(\\)and forward slash(//) are acceptable to separate the elements in a path.

import os
os.chdir('C:\\Users\\Desktop') 

Now the working directory changed from the python folder to Desktop. You can check the change in the directory by simply listing the elements inside the directory.

How to list files or directories

We can list the elements inside the directories or files using the listdir() method. If you have not passed the argument then it will provide all elements in the current directory. The return will be a list containing all directories or files in the specified directory. Here, in our case, all folders and files on the desktop are displayed as a list.

Observe the following example :

import os
os.chdir('C:\\Users\\Desktop')
os.listdir() 

The output will be:

['desktop.ini', 'EMAIL TEMPLATES.docx', ' 'New Microsoft Excel Worksheet.xlsx', 'New Microsoft Word Document (2).docx', 'New Microsoft Word Document.docx', 'notepad.py', 'PY PRO']

How to create a directory

To create a directory, we use the built-in method mkdir(). We should pass the path of the directory as its argument.

import os
os.mkdir('C:\\Users\\Desktop\\Directory_1') 

Output:

create a directory

If you have tried to create a directory that already exists in the system, then you will be getting an error stating FileExistsError as illustrated below.

import os
os.mkdir('C:\\Users\\Desktop\\PY PRO') 

Output:

Traceback (most recent call last):
  File "", line 1, in 
    os.mkdir('C:\\Users\\Desktop\\PY PRO')
FileExistsError: [WinError 183] Cannot create a file when that file 

Just take a look at the output of listdir() where you can see the “PY PRO” folder is already available in the system.

Python also has a built-in method called makedirs() which is used to create directories in a nested structure.In other words makedirs() the method creates directories recursively as illustrated in the below example:

import os
os.makedirs('PYTHON/DataTypes/int')
os.makedirs('C/DataTypes/float')
os.makedirs('Java/DataTypes/float')
os.listdir() 

Output:

['C', 'JAVA', 'PYTHON']

This will create a path in the directory even if it does not find level 1 folders or directories. It nests the file in the order we have given to them. In this example Level 0 folder is Directory_1, a folder PYTHON is created at Level 1 which inturn creates another folder named DataTypes at level 2, and at level 3 it contains the folder int. But in the above os.listdir()has listed only directories in a specific level.

Hierarchical structure of the Directory

The hierarchical structure of the Directory

Traverse Directory Recursively

To list the nested structure of a directory we can use the method os.walk() which allows us to traverse a directory recursively. The os.walk() method returns the roots, subdirectories, and files in a directory with the help of for loop. The following example prints all the subdirectories and files in the Directory_1.

import os
rootdir='C:\\Users\\TP-E540\\Desktop\\Programming Languages'
for roots,dirs,files in os.walk(rootdir):
 print("{0} has {1} files".format(roots, len(files))) 

Output:

Output:
C:\Users\Desktop\Programming Languages has 8 files
C:\Users\Desktop\Programming Languages\C has 2 files
C:\Users\Desktop\Programming Languages\JAVA has 2 files
C:\Users\Desktop\Programming Languages\PYTHON has 2 files
C:\Users\Desktop\Programming Languages\PYTHON\DataTypes has 1 files
C:\Users\Desktop\Programming Languages\PYTHON\DataTypes\float has 1 file

How to rename a directory?

Suppose you want to rename a directory, then you can use the method rename(source,destination) which is available in the os module. This rename() method requires two arguments,

Source_name: denotes the name to be changed

Destination_name: denotes the new name

import os
os.rename('Directory_1','Programming Languages') 

Output:

Rename Directory

In the example, initially, the name of the folder was Directory_1 which is renamed to Programming Languages.

When the source directory  ‘Directory_1’ was not available in the system, then the system will raise an exception named FileNotFoundError. Similarly, when the destination ‘Programming Languages’ already exists in the system, again a FileExistsError exception is raised.

How to delete a directory

We can delete a directory from the system using the method rmdir() where the argument passed is the path of the directory

import os
os.rmdir('C:\\Users\\Desktop\\IMAGES') 

This example implies the deletion of an empty folder or directory named IMAGES. The folder will be deleted from the system.

What will happen if we again attempt to delete the deleted folder or the folder which is not present in the system. Observe the below example:

import os
os.rmdir('C:\\Users\\Desktop\\IMAGES') 

Output:

Traceback (most recent call last):
  File "", line 1, in 
    os.rmdir('C:\\Users\\Desktop\\IMAGES')
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\Desktop\\IMAGES'

Obviously FileNotFoundError will raise as the file is not available in the system.

Consider the scenario where we attempt to delete a non-empty directory, which means a directory consisting of subfolders or files. The interpreter will raise an OSError as illustrated below:

import os
os.rmdir('C:\\Users\\Desktop\\Programming Languages') 

Output:

Traceback (most recent call last):
  File "", line 1, in 
    os.rmdir('C:\\Users\\Desktop\\Programming Languages')
OSError: [WinError 145] The directory is not empty: 'C:\\Users\Desktop\\Programming Languages'

Path joining and Splitting

So far we have discussed how to manipulate a directory or file in a single platform. To run our program irrespective of the platform, we have to use platform-independent directory or file paths. This can be accomplished with the use of submodule os.path.

Two important methods associated with os.path submodule are :

  1. join() - joins the path elements in python
    >>> import os
    >>> os.path.join('C:','Users','Desktop','Programming Languages') 
    
  2. split() - splits the path element in python.
    >>> import os
    >>> os.path.split('C:\\Users\\Desktop\\Programming Languages')
    ('C:\\Users\\Desktop', 'Programming Languages') 
    

Checking Directory Existence

It is also possible to check the existence of a path in the system. To check whether a directory exists in the system we can use  either the exists() function or isdir() function.Both these functions reside in the submodule os.path. The following example shows how exists() function checks for the existence of a path.

>>> import os
>>> os.path.exists('C:\\Users\\TP-E540\\Desktop\\Programming Languages')
True
>>> os.path.exists('C:\\Users\\TP-E540\\Desktop\\File.txt')
True
>>> os.path.exists('C:\\Users\\TP-E540\\Desktop\\New')
False 

The isdir() function checks the existence of a path to a directory, not to a file. The following example will clarify this :

>>> import os
>>> os.path.isdir('C:\\Users\\TP-E540\\Desktop\\Programming Languages')
True
>>> os.path.isdir('C:\\Users\\TP-E540\\Desktop\\New.txt')
False
>>> os.path.isdir('C:\\Users\\TP-E540\\Desktop\\New dir')
False 

So we can summarize that exists() function checks if the path exists or not. While the isdir() function checks if the path to a directory exists or not.