Python os Module Explained: Complete Guide With Examples

 Python os Module Explained: A Complete Beginner’s Guide

Python is one of the most popular programming languages for automation. It can help you perform repetitive tasks, manage data, create reports, and work with files and folders automatically.


Python os module tutorial showing operating system and file management automation.

But have you ever wondered how Python interacts with your computer's operating system?

For example, suppose you have hundreds of files on your computer. You want to create folders, rename files, check file locations, delete unwanted files, or organize documents automatically.

Doing these tasks manually can take a lot of time.

Python provides a built-in module called os that makes many of these operations easier.

The Python os module allows you to interact with the operating system and perform tasks such as managing directories, accessing file paths, checking files, and working with environment variables.

In this beginner-friendly guide, you will learn what the Python os module is, how it works, its important functions, and how to use it in real-world automation projects.

By the end of this tutorial, you will understand how to use the os module to automate common file and folder management tasks.

1. What Is the Python os Module?

The Python os module is a built-in Python module that allows programs to interact with the operating system.

It provides functions to work with files, folders, directories, paths, and environment variables.

For example, you can use the os module to:

  • Create new folders.
  • Delete empty folders.
  • Rename files and directories.
  • Check whether a file exists.
  • Get the current working directory.
  • List files inside a folder.
  • Create directory structures.
  • Access environment variables.
  • Manage file paths.

You do not need to install the os module separately because it comes with Python.

Simple Example

import os

print(os.getcwd())

Output example:

C:\Users\YourName\Documents\Python

This code displays the current working directory of your Python program.

The exact output depends on where you run the script.

2. How to Import the os Module in Python

Before using the functions of the os module, you need to import it.

Important Python os module functions and their uses.

Syntax:

import os

After importing it, you can access its functions using os.function_name().

For example:

import os

print(os.name)

This displays a short identifier for the operating system interface used by Python.

Common values include:

  • nt for Windows.
  • posix for Linux and macOS.

Important: os.name identifies the operating system interface, not the full operating system version.

3. How to Get the Current Working Directory Using os.getcwd()

A working directory is the folder from which your Python program is currently operating.

Python provides the os.getcwd() function to get its location.

Example:

import os

current_path = os.getcwd()

print("Current Working Directory:")
print(current_path)

Output example:

Current Working Directory:
C:\Users\YourName\Documents\Python

Why Is os.getcwd() Useful?

It helps you understand where Python is searching for files when you provide relative file paths.

For example, if you try to open data.txt without providing a complete path, Python normally looks for it relative to the current working directory.

This function is useful when working with file automation projects.

4. How to Change the Working Directory Using os.chdir()

Sometimes you need to change the folder in which your Python program operates.

Getting and changing the current working directory using Python.

Python provides the os.chdir() function for this purpose.

Example:

import os

os.chdir("C:/Users/YourName/Documents")

print(os.getcwd())

This changes the current working directory to the Documents folder.

Important Points

  • The specified directory must exist.
  • Python raises an error if the directory cannot be accessed.
  • Changing the working directory affects relative paths used later in the program.

You can also handle errors using exception handling.

import os

try:
    os.chdir("C:/Users/YourName/Documents")
    print("Directory changed successfully.")

except FileNotFoundError:
    print("Directory not found.")

except PermissionError:
    print("Permission denied.")

5. How to List Files and Folders Using os.listdir()

The os.listdir() function returns the names of files and folders inside a specified directory.

Example:

import os

files = os.listdir("C:/Users/YourName/Documents")

print(files)

Output example:

['report.xlsx', 'image.png', 'notes.txt', 'Python']

The output is a list containing the names of the directory entries.

List Files From the Current Directory

You can also use os.listdir() without providing a path.

import os

items = os.listdir()

for item in items:
    print(item)

This displays the files and folders in the current working directory.

Check Whether an Item Is a File or Folder

You can combine os.listdir() with os.path.isfile() and os.path.isdir().

import os

folder = "C:/Users/YourName/Documents"

for item in os.listdir(folder):

    path = os.path.join(folder, item)

    if os.path.isfile(path):
        print("File:", item)

    elif os.path.isdir(path):
        print("Folder:", item)

This helps you identify files and folders separately.

6. How to Create a Folder Using os.mkdir()

Python provides the os.mkdir() function to create a new directory.

Example:

import os

os.mkdir("MyFolder")

print("Folder created successfully.")

This creates a folder named MyFolder inside the current working directory.

Create a Folder at a Specific Location

import os

folder_path = "C:/Users/YourName/Documents/PythonProjects"

os.mkdir(folder_path)

print("Folder created successfully.")

Important: The parent directory must already exist, and the folder must not already exist.

If you try to create an existing folder with os.mkdir(), Python raises a FileExistsError.

7. How to Create Multiple Folders Using os.makedirs()

Sometimes you need to create multiple folders and subfolders together.

Creating folders and subfolders using Python os module.

For example:

PythonProjects/
    ExcelAutomation/
        Reports/

You can use os.makedirs() to create the complete directory structure.

Example:

import os

folder_path = "PythonProjects/ExcelAutomation/Reports"

os.makedirs(folder_path)

print("Folders created successfully.")

This creates the parent and child directories if they do not already exist.

Use exist_ok=True

If you want to avoid an error when the directory already exists, use exist_ok=True.

import os

folder_path = "PythonProjects/ExcelAutomation/Reports"

os.makedirs(folder_path, exist_ok=True)

print("Directory is ready.")

This is useful in automation scripts that may run multiple times.

8. How to Remove a Folder Using os.rmdir()

Python provides the os.rmdir() function to remove an empty directory.

Example:

import os

os.rmdir("MyFolder")

print("Folder removed successfully.")

Important Points

  • The folder must exist.
  • The folder must be empty.
  • Python raises an error if the folder contains files or other directories.

You can use exception handling to manage errors.

import os

try:
    os.rmdir("MyFolder")
    print("Folder deleted successfully.")

except FileNotFoundError:
    print("Folder does not exist.")

except OSError:
    print("Folder is not empty or cannot be removed.")

Remember: os.rmdir() removes only an empty folder. It does not delete the files inside it.

9. How to Rename Files and Folders Using os.rename()

The os.rename() function allows you to rename files and directories.

Example: Rename a File

import os

os.rename("old_report.txt", "new_report.txt")

print("File renamed successfully.")

This changes the file name from old_report.txt to new_report.txt.

Rename a Folder

import os

os.rename("OldFolder", "NewFolder")

print("Folder renamed successfully.")

You can also use complete file paths when working with files in different locations.

Always verify the source and destination paths before renaming important files.

10. How to Delete a File Using os.remove()

The os.remove() function deletes a file.

Example:

import os

os.remove("old_data.txt")

print("File deleted successfully.")

Check Before Deleting a File

It is safer to check whether the file exists before deleting it.

import os

file_path = "old_data.txt"

if os.path.isfile(file_path):

    os.remove(file_path)

    print("File deleted successfully.")

else:
    print("File does not exist.")

Important: File deletion can be permanent. Check the path carefully before running deletion scripts, especially when working with business data.

11. How to Check Whether a File or Folder Exists

The os.path.exists() function checks whether a given path exists.

Example:

import os

path = "data.xlsx"

if os.path.exists(path):
    print("Path exists.")

else:
    print("Path does not exist.")

Check Whether a Path Is a File

Use os.path.isfile().

import os

path = "data.xlsx"

if os.path.isfile(path):
    print("This is a file.")

else:
    print("File not found.")

Check Whether a Path Is a Directory

Use os.path.isdir().

import os

path = "Reports"

if os.path.isdir(path):
    print("This is a directory.")

else:
    print("Directory not found.")

These functions are especially useful when building automation scripts that need to check files before performing operations.

12. How to Join File Paths Using os.path.join()

When working with files and folders, you often need to combine directory names and file names.

The os.path.join() function helps create file paths using the correct path separator for the operating system.

Example:

import os

folder = "Reports"

file_name = "sales.xlsx"

file_path = os.path.join(folder, file_name)

print(file_path)

Possible output on Windows:

Reports\sales.xlsx

Possible output on Linux or macOS:

Reports/sales.xlsx

Why Use os.path.join()?

It makes your code more portable across operating systems.

Instead of manually adding / or \, you can let Python build the path.

Join Multiple Folder Names

import os

file_path = os.path.join(
    "PythonProjects",
    "ExcelAutomation",
    "Reports",
    "sales.xlsx"
)

print(file_path)

This is useful when building automated folder structures.

13. How to Get the File Name and Extension Using os.path

Python provides several useful functions for extracting information from file paths.

Understanding file paths and extensions with Python os module.

A. Get the File Name Using basename()

import os

path = "C:/Users/YourName/Documents/report.xlsx"

print(os.path.basename(path))

Output:

report.xlsx

B. Get the Directory Name Using dirname()

import os

path = "C:/Users/YourName/Documents/report.xlsx"

print(os.path.dirname(path))

Output:

C:/Users/YourName/Documents

C. Get the File Extension Using splitext()

import os

path = "report.xlsx"

file_name, extension = os.path.splitext(path)

print("File Name:", file_name)
print("Extension:", extension)

Output:

File Name: report
Extension: .xlsx

These functions are helpful when you want to organize files based on their names or extensions.

14. How to Get File Information Using os.stat()

Sometimes you need information about a file, such as its size or modification time.

Python provides os.stat() for this purpose.

Example:

import os

file_path = "report.xlsx"

if os.path.exists(file_path):

    file_info = os.stat(file_path)

    print("File Size:", file_info.st_size, "bytes")

    print("Last Modified:", file_info.st_mtime)

else:
    print("File not found.")

The st_size attribute returns the file size in bytes.

The st_mtime attribute returns the last modification time as a Unix timestamp.

You can convert this timestamp into a readable date.

import os
from datetime import datetime

file_info = os.stat("report.xlsx")

modified_time = datetime.fromtimestamp(
    file_info.st_mtime
)

print("Last Modified:", modified_time)

This can be useful when checking whether a report has been updated recently.

15. How to Get Environment Variables Using os.environ

Environment variables store configuration information used by applications and operating systems.

Python allows you to access them using os.environ.

Example:

import os

print(os.environ)

This displays the environment variables available to the current Python process.

Get a Specific Environment Variable

import os

username = os.environ.get("USERNAME")

print("Username:", username)

On Windows, USERNAME is commonly available.

On Linux and macOS, USER is commonly used instead.

Set an Environment Variable

import os

os.environ["APP_NAME"] = "ITTechLanguage"

print(os.environ.get("APP_NAME"))

This sets an environment variable for the current Python process and its child processes.

It does not permanently change your operating system's environment settings.

Environment variables are often used to store application settings and configuration values. Avoid printing sensitive values such as passwords or API keys.

16. How to Find All Files Inside a Folder Using os.walk()

The os.walk() function is useful when you want to explore a directory and its subdirectories.

It generates information about folders, subfolders, and files.

Example:

import os

folder_path = "PythonProjects"

for root, directories, files in os.walk(folder_path):

    print("Current Folder:", root)

    print("Subfolders:", directories)

    print("Files:", files)

    print("----------------------")

How Does os.walk() Work?

It returns three values during each iteration:

  • root: The current directory path.
  • directories: A list of subdirectory names.
  • files: A list of file names inside the current directory.

This function is useful when working with large folder structures.

Find Only PDF Files

import os

folder_path = "Documents"

for root, directories, files in os.walk(folder_path):

    for file in files:

        if file.lower().endswith(".pdf"):

            print(os.path.join(root, file))

This script searches through the folder and its subfolders and displays all PDF file paths.

17. Practical Python Project: Automatically Organize Files by Extension

Now let's build a practical project using the Python os module.

Suppose your Downloads folder contains different types of files:

  • PDF documents.
  • Excel workbooks.
  • Images.
  • Text files.
  • Word documents.

You want to organize these files into separate folders automatically.

Python can perform this task using os, along with the shutil module.

Project Requirements

The program will:

  1. Read files from a source folder.
  2. Identify each file's extension.
  3. Create a folder for that extension.
  4. Move the file into the correct folder.
  5. Display a completion message.

Python Code

import os
import shutil

source_folder = "Downloads"

if not os.path.isdir(source_folder):

    print("Source folder does not exist.")

else:

    for file in os.listdir(source_folder):

        source_path = os.path.join(
            source_folder,
            file
        )

        if os.path.isfile(source_path):

            extension = os.path.splitext(file)[1]

            if extension:

                folder_name = extension[1:].upper()

            else:

                folder_name = "OTHER"

            destination_folder = os.path.join(
                source_folder,
                folder_name
            )

            os.makedirs(
                destination_folder,
                exist_ok=True
            )

            destination_path = os.path.join(
                destination_folder,
                file
            )

            if os.path.exists(destination_path):

                print("Skipped existing file:", file)

                continue

            shutil.move(
                source_path,
                destination_path
            )

            print("Moved:", file)

    print("File organization completed.")

Example Folder Structure

Before running the program:

Downloads/
    report.xlsx
    photo.png
    notes.txt
    invoice.pdf
    document.docx

After running the program:

Downloads/
    XLSX/
        report.xlsx

    PNG/
        photo.png

    TXT/
        notes.txt

    PDF/
        invoice.pdf

    DOCX/
        document.docx

What Did We Learn?

This project combines several Python concepts:

  • os.listdir() to list files.
  • os.path.join() to create file paths.
  • os.path.splitext() to identify extensions.
  • os.makedirs() to create folders.
  • os.path.exists() to check destination files.
  • shutil.move() to move files.

Safety note: This script changes the location of files. Test it first with sample files in a separate folder. It also skips files when a file with the same name already exists in the destination.

18. Difference Between os and pathlib Modules

Python provides another module called pathlib for working with file system paths.

Both os and pathlib are useful, but their approaches are slightly different.

Featureos Modulepathlib Module
Get current directoryos.getcwd()Path.cwd()
Join pathsos.path.join()/ operator
Check existenceos.path.exists()Path.exists()
Check fileos.path.isfile()Path.is_file()
Check directoryos.path.isdir()Path.is_dir()
Create directoryos.mkdir()Path.mkdir()
Rename fileos.rename()Path.rename()

Example Using pathlib

from pathlib import Path

folder = Path("Reports")

folder.mkdir(exist_ok=True)

print("Folder created or already exists.")

Which One Should You Learn?

Both are important.

Learn the os module because it is widely used in Python automation and provides many operating-system functions.

Also learn pathlib because it offers a convenient, object-oriented way to work with file paths.

19. Common Mistakes Beginners Should Avoid

When using the Python os module, beginners often make these mistakes.

1. Forgetting to Import os

Always import the module before using its functions.

import os

2. Using Incorrect File Paths

If a file or folder path is incorrect, Python may raise FileNotFoundError.

Check the path before performing an operation.

3. Creating an Existing Folder

Using os.mkdir() on an existing folder raises FileExistsError.

Use os.makedirs(path, exist_ok=True) when appropriate.

4. Trying to Delete a Non-Empty Folder With os.rmdir()

The os.rmdir() function only removes empty directories.

5. Confusing Files and Folders

Use os.path.isfile() to check files and os.path.isdir() to check directories.

6. Deleting Files Without Checking

Always verify the file path before calling os.remove().

7. Ignoring Operating System Differences

Windows, Linux, and macOS may use different path separators and environment variables.

Use functions such as os.path.join() to make your scripts more portable.

20. Real-World Applications of the Python os Module

The os module is useful in many real-world situations.

1. File Management Automation

Automatically create folders, rename files, and organize documents.

2. Excel Report Management

Create report folders, check whether files exist, and prepare directories for daily MIS reports.

3. Data Analyst Projects

Find source files, check folder structures, and manage datasets before processing them with Pandas.

4. Backup Management

Create backup directories and organize important documents.

5. Application Development

Access environment variables and manage application configuration.

6. Log File Management

Create directories for log files and manage their locations.

7. Business Operations

Organize seller reports, delivery records, employee data, and operational documents.

Frequently Asked Questions (FAQs)

1. What is the os module in Python?

The os module is a built-in Python module used to interact with the operating system and manage files, folders, paths, and environment variables.

2. Do I need to install the os module?

No. The os module comes with Python. You only need to import it using import os.

3. What is os.getcwd() used for?

The os.getcwd() function returns the current working directory of your Python program.

4. What is the difference between os.mkdir() and os.makedirs()?

os.mkdir() creates one directory, while os.makedirs() can create multiple levels of nested directories.

5. How can I list all files in a folder using Python?

You can use os.listdir() to list the entries inside a folder.

6. How can I delete a file using Python?

You can use os.remove() to delete a file.

7. What is the use of os.path.join()?

It combines folder and file names into a path using the correct separator for the operating system.

8. What is os.walk() used for?

os.walk() searches through a directory and its subdirectories, providing information about folders and files.

9. What is the difference between os and pathlib?

The os module provides operating-system functions and path utilities. The pathlib module provides an object-oriented approach to working with file system paths.

10. Is the Python os module useful for automation?

Yes. It is useful for file organization, folder creation, report management, directory scanning, and other automation tasks.

Conclusion

The Python os module is an important tool for anyone learning Python programming and automation.

It allows you to interact with your computer's operating system directly from Python code.

In this tutorial, we covered important functions such as os.getcwd(), os.chdir(), os.listdir(), os.mkdir(), os.makedirs(), os.remove(), os.rename(), os.path.join(), and os.walk().

We also created a practical file organization project to understand how these functions work together.

Start with simple examples and practise creating folders, listing files, checking paths, and organizing documents. Once you understand these concepts, you can build more advanced Python automation projects.

Keep learning and keep automating with ITTech Language!

Post a Comment

Ask any query by comments

Previous Post Next Post