GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Python Directory Management - GeeksforGeeks Python Directory ManagementLast Updated : 6 Apr, 2026 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. Why do we need Directory Management
os and os.path moduleos module provides functions to interact with the operating system, such as managing directories, files, processes and environment variables. Creating new directoryA new directory can be created using below function:
Example: This code creates one simple directory and one nested directory.
Explanation:
Getting Current Working Directory (CWD)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.
Output String format : /home/guest/sandbox Byte string format : b'/home/guest/sandbox' Explanation:
Renaming a directoryTo 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:
Changing Current Working Directory (CWD)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
Explanation:
Listing Files in a DirectoryYou can get the names of all files and folders inside a directory.
Example: This code lists everything in the current directory.
Output 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()). Removing a directoryDirectories 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:
Check Whether It Is a DirectoryBefore 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.
Output True False Working with Directories using pathlibpathlib 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:
Get Size of the DirectoryTo 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
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. Getting Access and Modification TimesTo retrieve the last access and modification times of files or directories use below functions:
Example : Getting access and modification time of GeeksforGeeks (root) directory
Output 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 moduleshutil module is a high-level file and directory management library. It provides functions for copying, moving and removing files and directories. shutil.copytree()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:
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. shutil.rmtree()Deletes an entire directory tree, including all its files and subdirectories. This operation is irreversible. Be careful when specifying the path. Syntax:
Parameters:
Example: This code permanently deletes the destination_dir folder and everything inside it.
Explanation: Recursively deletes destination_dir and all its contents.
shutil.move(s,d)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:
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. Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |