Python os Module Overview

The os module helps Python work with your operating system. Beginners often use it for files, folders, paths, and environment information.

It is part of Python’s standard library, so you do not need to install anything.

This page is an overview of what os does and when to use it. It is not a full guide to every function in the module.

Quick example

import os

print(os.getcwd())
print(os.listdir())

This quick example:

  • imports os
  • shows the current working directory
  • lists files and folders in that directory

What the os module is

The os module lets your Python code interact with the operating system.

You will often see it used for tasks like:

  • finding the current folder
  • listing files and directories
  • changing directories
  • checking whether a path exists
  • working with environment variables

Because os is built into Python, you can use it right away:

import os

What beginners use os for

Beginners usually use os for a few common tasks:

  • Get the current working directory
  • List files and folders
  • Change the current directory
  • Check whether a path exists
  • Join path parts safely
  • Access environment variables

For example:

Commonly used os functions

Here are some of the most common functions beginners use.

os.getcwd()

Returns the current working directory.

import os

current_folder = os.getcwd()
print(current_folder)

If you want a full beginner explanation, see os.getcwd() explained.

os.listdir()

Returns the names inside a directory.

import os

items = os.listdir()
print(items)

This usually returns names only, not full paths.

For more detail, see os.listdir() explained.

os.chdir()

Changes the current working directory.

import os

os.chdir("my_folder")
print(os.getcwd())

Be careful with this. After changing directories, relative paths will point somewhere else.

For syntax and examples, see os.chdir() explained.

os.path.join()

Builds a path from multiple parts safely.

import os

file_path = os.path.join("projects", "notes.txt")
print(file_path)

This is better than joining path parts by hand with slashes.

See os.path.join() explained.

os.path.exists()

Checks whether a file or folder exists.

import os

print(os.path.exists("notes.txt"))

If you want to solve that task step by step, read how to check if a file exists in Python.

Basic example

Here is a short runnable example that imports os, prints the current folder, and lists the names in that folder.

import os

print("Current folder:")
print(os.getcwd())

print("\nNames in this folder:")
for name in os.listdir():
    print(name)

What this code does

  • import os makes the module available
  • os.getcwd() returns the current working directory
  • os.listdir() returns the names in that directory
  • the for loop prints each name on its own line

Example output

Current folder:
/Users/sam/projects/python-demo

Names in this folder:
main.py
notes.txt
images

Your output will be different because it depends on your own folder.

os vs os.path

os and os.path are related, but they are not the same thing.

os

The main os module contains general operating system tools, such as:

  • os.getcwd()
  • os.listdir()
  • os.chdir()
  • environment variable access

os.path

os.path focuses on file and folder paths, such as:

  • os.path.join()
  • os.path.exists()

Beginners often use both together in the same script.

For example:

import os

folder = os.getcwd()
file_name = "example.txt"
full_path = os.path.join(folder, file_name)

print(full_path)
print(os.path.exists(full_path))

Use os.path.join() instead of building paths by hand. This helps your code work more reliably across different systems.

If you want a broader explanation, see working with file paths in Python.

When to use this page vs other pages

Use this page when you want a quick overview of what the os module does.

Use other pages when you need something more specific:

  • Use function pages for exact syntax, parameters, and return values
  • Use how-to pages for practical tasks
  • Use error pages when something is going wrong

For example:

Common beginner mistakes

These are some common problems beginners run into when using os.

Forgetting to import os

This will fail:

print(os.getcwd())

You need:

import os

print(os.getcwd())

Using hard-coded slashes in paths

Avoid building paths like this:

path = "folder/subfolder/file.txt"

Instead, use:

import os

path = os.path.join("folder", "subfolder", "file.txt")
print(path)

Assuming os.listdir() returns full paths

os.listdir() usually returns names only:

import os

items = os.listdir()
print(items)

If you need a full path, join the directory and the item name:

import os

folder = os.getcwd()

for name in os.listdir(folder):
    full_path = os.path.join(folder, name)
    print(full_path)

Changing directories and losing track of where you are

If you use os.chdir(), your script may start looking in a different folder than you expected.

A simple way to check is:

import os

print(os.getcwd())

Mixing up files and directories

A path can point to either a file or a folder. If your code assumes the wrong one, it may fail.

It helps to print the path you are checking and confirm it is the one you expect.

FAQ

What is the os module in Python?

It is a standard library module that helps Python interact with the operating system, especially files, folders, and paths.

Do I need to install the os module?

No. It comes with Python.

What is the difference between os and os.path?

os includes general operating system tools. os.path is used specifically for working with paths.

Should I use os or pathlib?

Both work. Beginners often see os in many examples, but pathlib is also a good modern option for path handling.

Does os.listdir() return full file paths?

No. It usually returns names only. You often need os.path.join() to create a full path.

See also