Back to Python
2026-03-195 min read

Example: Using the OS Module (Python Programming)

Learn Example: Using the OS Module (Python Programming) step by step with clear examples and exercises.

Title: Mastering Python's OS Module - A full guide

Why This Matters

The os module is a fundamental tool in Python that allows you to interact with the operating system, execute system commands, and manipulate files and directories. Understanding this module can help you automate tasks, write scripts for system administration, and build robust applications. It serves as the foundation for more advanced modules like subprocess and psutil.

Prerequisites

  • Basic understanding of Python programming concepts such as variables, data types (strings, lists, dictionaries), control structures (if-else, loops), and functions.
  • Familiarity with file handling in Python is also beneficial but not strictly required.

Core Concept

The os module provides a way to use operating system dependent functionality. It is built into the Python standard library and doesn't require any additional installation. Here are some key functions in the OS module:

File and Directory Operations

  1. os.getcwd() - Returns the current working directory as a string.
  2. os.chdir(path) - Changes the current working directory to the specified path. If the path is relative, it will be interpreted based on the current working directory.
  3. os.listdir(path) - Lists all entries (files and directories) in the specified path as a list of strings.
  4. os.mkdir(name) - Creates a new directory with the given name in the current working directory if it doesn't already exist. If you want to create a directory at an absolute or relative path, use os.makedirs(path, mode=0o777, exist_ok=False).
  5. os.remove(name) - Deletes the file or directory with the given name from the current working directory if it exists. For directories, they must be empty before being removed. Use shutil.rmtree(path) to remove a non-empty directory along with its contents.
  6. os.rename(src, dst) - Renames the source file or directory to the destination name. If the destination already exists, it will be replaced.
  7. os.path.join(path, *paths) - Joins one or more path components intelligently, handling platform-specific differences in path separators.
  8. os.path.exists(path) - Checks if a file or directory exists at the specified path.
  9. os.path.isfile(path) and os.path.isdir(path) - Checks if the given path is a file or a directory, respectively.
  10. os.path.abspath(path) - Returns the absolute path of the specified path.

System Commands Execution

  1. os.system(command) - Executes the command in the operating system shell and returns its exit status. This function should be used with caution as it can potentially execute arbitrary commands.

Worked Example

Let's create a simple script that lists all files in the current directory, changes to a subdirectory, creates a new file, and then removes it if it exists. We will also demonstrate handling exceptions when executing system commands.

import os
import sys

List all files in the current directory

print("Files in the current directory:")

files = os.listdir(os.getcwd())

for file in files:

print(file)

try:

Change to a subdirectory named 'subdir' if it exists, otherwise create it

if os.path.exists('subdir'):

os.chdir('subdir')

else:

os.makedirs('subdir')

os.chdir('subdir')

Create a new file named 'example.txt' with some content

with open('example.txt', 'w') as f:

f.write("This is an example file.\n")

If the file exists, remove it and print a message

if os.path.isfile('example.txt'):

os.remove('example.txt')

print("Removed example.txt from the current directory.")

else:

print("No example.txt found in the current directory.")

except FileNotFoundError as e:

print(f"An error occurred while executing the command: {e}")

Common Mistakes

  1. Forgetting to import the os module - Always start your script with import os.
  2. Not using os.path.join() correctly - Make sure to use it when joining paths, as it handles platform-specific differences in path separators.
  3. Trying to remove a non-existent file or directory - Use functions like os.path.exists(), os.path.isfile(), and os.path.isdir() to check if the item exists before attempting to remove it.
  4. Not handling exceptions - When using os.system(command), make sure to handle potential exceptions, such as when the command is not found or returns an error code.
  5. Misusing os.remove() and shutil.rmtree() - Be aware that os.remove() only works for files and directories that are empty, while shutil.rmtree() removes a directory along with its contents.
  6. Not checking the return value of os.system(command) - The return value can indicate whether the command was executed successfully or not.

Subheadings under Common Mistakes:

  • Using absolute paths inappropriately
  • Not handling path separators correctly

Practice Questions

  1. Write a script that creates a new directory named 'test' and changes to it.
  2. Write a script that lists all files in the parent directory of the current directory.
  3. Write a script that removes a file named 'file.txt' from the current directory if it exists, and prints an error message otherwise.
  4. Write a script that renames a file named 'oldname.txt' to 'newname.txt' in the current directory if both files exist.
  5. Write a script that finds all subdirectories in the current directory and lists their names.
  6. Write a script that searches for a specific file or directory recursively in the current directory and its subdirectories.
  7. Write a script that moves a file named 'source.txt' to a new directory named 'destination'.
  8. Write a script that counts the number of files and directories in the current directory.
  9. Write a script that executes the ls command and prints its output.
  10. Write a script that finds all Python scripts (files with .py extension) in the current directory and its subdirectories.

FAQ

A: You should use os.rmdir(path) to remove an empty directory, and shutil.rmtree(path) to remove a non-empty directory along with its contents.

Q: Can I execute system commands without using the shell in os.system()?

A: Yes, you can use the subprocess module for more control over executing system commands. However, it is more complex than os.system().

Q: How do I get the absolute path of a file or directory?

A: You can use os.path.abspath(path) to get the absolute path of a given path.

Q: Is there a function to walk through directories and process each file?

A: Yes, you can use the os.walk() function to recursively traverse a directory structure.

Q: How do I check if a process is running on my system?

A: You can use the psutil library for more advanced system monitoring tasks like checking processes. It's not part of the standard library, so you'll need to install it using pip.

Example: Using the OS Module (Python Programming) | Python | XQA Learn