Python Os Module Tutorial – Complete Guide

Venturing into the world of Python programming introduces us to various powerful tools and modules. One such module that carries an immense value for any Python programmer is the ‘os’ module. To dive deeper and comprehend the wonders it can do, let us embark on this exciting journey of exploring the Python ‘os’ module.

What is Python ‘os’ Module?

The Python ‘os’ module is essentially an interface that allows you to interact with the underlying operating system. It offers a range of functionalities to work with the OS in a smooth and efficient way.

The ‘os’ module unlocks an array of opportunities for you. It allows you:

  • To modify and deal with files and directories
  • Interact with environment variables
  • Perform different OS tasks like reading or writing to the UNIX pipe, determine the PID, and more

Why Should I learn it?

Learning the Python ‘os’ module can significantly enhance your coding skills and flexibility with Python. It is a great way to:

  • Manipulate, navigate, and manage directories and files efficiently
  • Automate and streamline OS-related tasks with ease
  • Dwell deeper into the realms of Python

With this, we aim to make you proficient in dealing with the file system, OS, and environmental variables, and aid in your transition from a traditional Python coder to an efficient Python programmer. Now let’s dive into the first half of our coding tutorial to unveil the power of the Python ‘os’ module.

CTA Small Image

Python ‘os’ Module: Basic Operations

Let’s start with some fundamental functions that the ‘os’ module offers.

Firstly, the os module needs to be imported into your Python environment. To do this, you can use the following command:

 import os

Let’s now look at some examples of operations you can perform with the ‘os’ module.

1. Operating system name: To get the name of the operating system, utilize ‘’.


File and Directory Operations

2. Current working directory: ‘os.getcwd()’ returns the current working directory.


3. Change working directory: ‘os.chdir(path)’ lets you modify the current working directory. Replace ‘path’ with your target directory.


4. List directory: ‘os.listdir()’ retrieves all files and sub-directories in the working directory.


Environment Variables

Environment variables yield useful details about your system. Here is how you can access them:

5. Access all environment variables: ‘os.environ’ provides all the environment variables.


6. Access specific environment variable: For a specific variable, use it like a dictionary.


Let’s now move to the third part of our tutorial which covers more advanced operations using Python’s ‘os’ module.

Creating and Deleting Directories

7. Create a directory: ‘os.mkdir()’ creates a new directory in the current working directory.


8. Removing directory: ‘os.rmdir()’ deletes a directory.


File Operations

9. Rename a file: Use ‘os.rename()’ to rename a file or a directory.

 os.rename('old_name.txt', 'new_name.txt')

10. Delete a file: ‘os.remove()’ can be used to remove or delete a file.


Path Operations

11. Joining Paths: ‘os.path.join()’ gives a concatenated string using the appropriate OS-dependent separator.

 print(os.path.join('/path/to/directory', 'file.txt'))

12. Check if Path Exists: ‘os.path.exists()’ checks if a given path exists or not.


13. Split Path and File: ‘os.path.split()’ separates the file or directory name at the end of a path.


With these advanced functions, the os module is truly an arsenal for every Python developer. It lets us interact with the operating system and perform various tasks with simplicity and efficiency. At Zenva, we are committed to empowering developers with the best tools and knowledge to ace their craft and keep learning.

Where to go next / How to keep learning

While you’ve made an excellent start with the Python ‘os’ module, remember, it’s only the tip of the iceberg. Python is a vast language with many modules, libraries, and frameworks, all of which can amplify your programming skills.

One such amazing resource to fuel your Python journey is our Python Mini-Degree program. This program is a comprehensive collection of meticulously designed Python courses. The Python Mini-Degree covers:

  • Basics of Python coding
  • Algorithms and Data Structures
  • Object-oriented programming
  • Game development
  • App development with Pygame, Tkinter, and Kivy

Coupled with interactive lessons, quizzes, and coding challenges, you will get ample opportunities to apply your learning and build a formidable portfolio of Python projects.

If you are eager to explore more, you can check out our wide collection of Python courses.


Python is an extensively versatile language, and the ‘os’ module is a testament to that. As we have seen, it allows us seamless access and control over the operating system and the file system. It simplifies many low-level tasks and brings efficiency to our Python programs. Yet, the depth of Python goes beyond the ‘os’ module. There are countless other modules, libraries, and frameworks waiting for you to explore.

With a determined mind and the right resource, you can indeed master Python. Our Python Mini-Degree program is that perfect resource you need to kickstart your Python journey. Enroll today to ensure a future where you are an accomplished Python developer, creating impactful solutions and leading others on the path of coding knowledge. Remember, every great journey starts with a single step!

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.