Skip to main content

Command Palette

Search for a command to run...

OS Modules in Python

The OS module in Python is a built-in library that provides functions for interacting with the operating system.

Published
3 min read
OS Modules in Python
S
I am a software engineer and full-stack developer specializing in the architecture of developer-centric tools, high-performance web applications, and automation systems. My work is defined by a focus on reducing technical complexity through better engineering, cleaner interfaces, and intuitive user experiences. Currently, I am deep in the development of agentic AI systems, crafting intelligent agents that automate complex, multi-step engineering workflows. I thrive on solving the unsolved problems, whether that involves architecting a scalable backend or designing a seamless interaction layer for a new tool.

Introduction

The os module in Python is a built-in library that provides functions for interacting with the operating system. It allows you to perform a wide variety of tasks, such as reading and writing files, interacting with the file system, and running system commands.

There are a lot of methods in the os module. But, here we will be discussing only the most important and commonly used methods of the os module.

Reading and Writing Files

The os module provides functions for opening, reading, and writing files. For example, to open a file for reading, you can use the open function:

import os

# Open the file in read-only mode
f = os.open("myfile.txt", os.O_RDONLY)

# Read the contents of the file
contents = os.read(f, 1024)

# Close the file
os.close(f)

To open a file for writing, you can use the os.O_WRONLY flag:

import os

# Open the file in write-only mode
f = os.open("myfile.txt", os.O_WRONLY)

# Write to the file
os.write(f, b"Hello, world!")

# Close the file
os.close(f)

Interacting with the File System

The os module also provides functions for interacting with the file system. For example, you can use the os.listdir function to get a list of the files in a directory:

import os

# Get a list of the files in the current directory
files = os.listdir(".")
print(files)  # Output: ['myfile.txt', 'otherfile.txt']

You can also use the os.mkdir function to create a new directory:

import os

# Create a new directory
os.mkdir("newdir")

Running System Commands

Finally, the os module provides functions for running system commands. For example, you can use the os.system function to run a command and get the output:

import os

# Run the "ls" command and print the output
output = os.system("ls")
print(output)  # Output: ['myfile.txt', 'otherfile.txt']

You can also use the os.popen function to run a command and get the output as a file-like object:

import os

# Run the "ls" command and get the output as a file-like object
f = os.popen("ls")

# Read the contents of the output
output = f.read()
print(output)  # Output: ['myfile.txt', 'otherfile.txt']

# Close the file-like object
f.close()

Conclusion

In summary, the os module in Python is a built-in library that provides a wide variety of functions for interacting with the operating system. It allows you to perform tasks such as reading and writing files, interacting with the file system, and running system commands. The above-discussed methods are the most commonly used and are very important to remember.

Well, that's a wrap for now!! Hope you folks have enriched yourself today with lots of known or unknown concepts. I wish you a great day ahead and till then keep learning and keep exploring!!

S
Sypher3y ago

Nice blog

13
S

It's awesome that you liked it 😊

8

Python : Advanced

Part 11 of 19

In this series, I will be discussing about some complicated/under-used features of Python, important libraries such as pandas; and also OOPs, Data Analytics and AI-ML.

Up next

The 3 Most Powerful Functions in Python

A brief description about the map(), filter() and reduce() functions