Module 4 Python
Module 4 Python
Module 4 Python
Directories in Python
Directories are folders used to organize files. Python’s os module provides
functions to work with directories:
Creating a directory: Use os.mkdir("directory_name") to create a new
directory.
Changing the directory: Use os.chdir("directory_name") to change the
current working directory.
Listing files in a directory: Use os.listdir("directory_name") to list all
files and directories inside a directory.
Deleting a directory: Use os.rmdir("directory_name") to remove an
empty directory.
Example:
import os
os.mkdir("new_folder") # Creating a new directory
os.chdir("new_folder") # Changing the current directory
print(os.listdir()) # Listing contents of the directory
os.rmdir("new_folder") # Deleting the directory