![]() |
VOOZH | about |
Directory Management involves performing operations such as creating, deleting, renaming, navigating and listing directories using code. It helps in handling folder structures programmatically using built-in modules like os, pathlib and shutil.
os module provides functions to interact with the operating system, such as managing directories, files, processes and environment variables.
A new directory can be created using below function:
Example: This code creates one simple directory and one nested directory.
Explanation:
Current Working Directory (CWD) is the location from which the Python script is being executed. To know where the current python script is running, use below function:
Example: This code prints the current working directory in two formats.
String format : /home/guest/sandbox Byte string format : b'/home/guest/sandbox'
Explanation:
To rename a folder or move it to a different location, the following functions are used:
Example: This code shows both simple renaming and renaming with path changes.
Explanation:
You can change where your script is working from. This helps when you want to run file operations inside a specific folder. os.chdir(path) changes the working directory to the given path.
Example: This code shows the directory before and after changing it.
Output
Before changing directory: /home/nikhil/Desktop/gfg
After changing directory: /home/nikhil/Desktop
Explanation:
You can get the names of all files and folders inside a directory.
Example: This code lists everything in the current directory.
Files in CWD are : ['output.txt', 'input.txt', 'driver', 'Solution.py']
Explanation: os.listdir() lists all files and folders in the current working directory (os.getcwd()).
Directories can be removed, but methods differ depending on whether they are empty or contain files.
Example: This code checks if a directory is empty before deleting it.
Explanation:
Before performing operations, it’s good practice to confirm if a given path is actually a directory. os.path.isdir(path) Returns True if the path is a directory, else False.
Example: This code checks two paths to see if they are directories.
True False
pathlib is a modern, object-oriented alternative to os and os.path.
Example: This code creates a folder and lists files in the current directory.
Explanation:
To calculate the size of a directory, you need to add up the sizes of all its files (recursively).
Example: This code computes the total size of the current directory.
Output
Total directory size: 23541 bytes
Explanation: os.walk() iterates through all files in the directory tree and os.path.getsize() adds up their sizes, giving the total size of contents.
To retrieve the last access and modification times of files or directories use below functions:
Example : Getting access and modification time of GeeksforGeeks (root) directory
Access Time: Sat Jan 4 09:21:37 2025 Modification Time: Sat Jan 4 09:21:37 2025
Explanation:time.ctime() converts the timestamp into human-readable format.
shutil module is a high-level file and directory management library. It provides functions for copying, moving and removing files and directories.
Recursively copies an entire directory tree (source directory and all its contents) to a destination. Creates a new directory at the destination path and copies all files and subdirectories. Raises FileExistsError if the destination exists and dirs_exist_ok is False.
Syntax:
shutil.copytree(src, dst, dirs_exist_ok=False)
Parameters:
Example: This code copies all files and folders from source_dir into destination_dir.
Explanation: Recursively copies the contents of source_dir (including files and subdirectories) into a new destination_dir.
Deletes an entire directory tree, including all its files and subdirectories. This operation is irreversible. Be careful when specifying the path.
Syntax:
shutil.rmtree(path, ignore_errors=False)
Parameters:
Example: This code permanently deletes the destination_dir folder and everything inside it.
Explanation: Recursively deletes destination_dir and all its contents.
Tip: If you want safer deletion (sending files to recycle bin instead of permanent removal), use send2trash module instead of shutil.rmtree().
Moves a file or directory to a new location. If the source and destination are on the same filesystem, this is equivalent to renaming. Otherwise, it copies the source to the destination and then deletes the original.
Syntax:
shutil.move(src, dst)
Parameters:
Example: This code moves the entire source_dir folder into new_location.
Explanation: Moves the entire source_dir to new_location. If both are on the same filesystem, it's a rename operation otherwise, it copies and deletes the source.