What is a Directory?
A method for organizing, storing, and separating data in a computer. Directories can be root or child directories. A root Directory is, that doesn’t have any parent. But a child directory has a parent and we use path(which gives the route to reach a file from the current location using slashes) to reach from one place to another.
There are many directories in python only. And we need a procedure to manage them. Python contains several files, modules, in-built functions, etc. and we use directories to manage, manipulate, and process them all.
With the help of Python, we can also interact with the OS of the system and its files. The modules used for this are –
- filecmp
- os and os.path
- shutil
- tempfile
Let’s discuss them one by one –
- os and os.path module – the module used to handle files and directories. It provides us with functions like create, rename, delete, knowing the current location, changing to another directory, and copying files from one directory to another.
Syntax:
os.mkdir(name) # it creates a new directory with the name you will pass in the name function.
By default, this new directory is created in the current working directory. But if we want to create it somewhere else, we can also do that as well.(just place the path before the name in the mkdir function with forward slash.)
COde:
Python Code
import os
os.mkdir('new_directory') # creates a directory in the current working directory.
Output:
- Getting the current working directory
Syntax:
os.getcwd()
It will return a string that represents a path of the current working directory.
Code:
Python Code
import os
os.getcwd()
Output:
- Renaming a directory
Syntax:
os.rename(‘old_name’ , ‘new_name’)
But if a directory with the new name is already present, then the directory will not be renamed and an error will be thrown.
Code:
Python Code
import os
os.rename('new_directory' ,'renamed_directory')
Output:
- Changing the current directory
Syntax:
os.chdir(path of the desired directory)
It takes the path of the desired directory.
- Listing the files in a directory – A directory may contain many subdirectories and files, and to list them, we use listdir() function.
Syntax:
os.listdir(the desired directory whose files need to be listed)
- Removing a directory– And we pass the path of the directory to be removed into the function.
Syntax:
os.rmdir(the desired directory to be removed)
- To check whether it is a directory – It returns boolean values(true or false) as per the result.
Syntax:
os.path.isdir(path of the directory)
- To get the size of the directory – And if we pass an invalid path, then an error is raised.
Syntax:
os.path.get_size(path of the directory)
Filecmp Module – this module provides functions to compare files and directories. For this, we create an object filecmp.dircmp which tells us to hide certain files and show certain files.
tempFile Module – this module is used to create temporary files and directories in the temp folder of the operating systems. In windows, the temp folder is located at – profile/AppData/Local/temp
Shutil module – it deals with high-level operations on directories and files. It allows copying/ moving content from the source directory to the destination directory.
Special thanks to Ishita Dhiman for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article. If you want to suggest any improvement/correction in this article please mail us at [email protected]