Skip to content

Python os Package: Overview and Examples

The os package in Python provides a way of using operating system-dependent functionality like reading or writing to the file system. This module provides a portable way of using operating system-dependent functionalities and is a part of the standard library.

Importing the os Module

To use the os module, you first need to import it.

import os

Commonly Used Functions

Working with Directories

os.getcwd()

Returns the current working directory.

current_directory = os.getcwd()
print(current_directory)

os.listdir()

Returns a list of entries in the specified directory.

entries = os.listdir('.')
print(entries)

os.mkdir()

Creates a new directory.

os.mkdir('new_directory')

os.makedirs()

Creates a new directory and any necessary parent directories.

os.makedirs('parent_dir/child_dir')

os.rmdir()

Removes an empty directory.

os.rmdir('new_directory')

os.removedirs()

Removes directories recursively.

os.removedirs('parent_dir/child_dir')

os.chdir()

Changes the current working directory.

os.chdir('new_directory')

Working with Files

os.rename()

Renames a file or directory.

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

os.remove()

Removes a file.

os.remove('new_name.txt')

Environment Variables

os.environ

A mapping object representing the string environment.

home_directory = os.environ.get('HOME')
print(home_directory)

os.getenv()

Gets the value of an environment variable.

path = os.getenv('PATH')
print(path)

os.putenv()

Sets the value of an environment variable.

os.putenv('MY_VAR', 'my_value')

Path Manipulations

os.path.join()

Joins one or more path components intelligently.

path = os.path.join('parent_dir', 'child_dir', 'file.txt')
print(path)

os.path.exists()

Returns True if the path exists.

exists = os.path.exists('path/to/file.txt')
print(exists)

os.path.isfile()

Returns True if the path is a regular file.

is_file = os.path.isfile('path/to/file.txt')
print(is_file)

os.path.isdir()

Returns True if the path is a directory.

is_dir = os.path.isdir('path/to/directory')
print(is_dir)

os.path.getsize()

Returns the size of the file in bytes.

size = os.path.getsize('path/to/file.txt')
print(size)

os.path.abspath()

Returns the absolute path of the specified path.

absolute_path = os.path.abspath('path/to/file.txt')
print(absolute_path)

Example: Creating a Directory, Writing a File, and Cleaning Up

import os

# Create a new directory
os.mkdir('example_dir')

# Change to the new directory
os.chdir('example_dir')

# Create and write to a new file
with open('example_file.txt', 'w') as file:
    file.write('Hello, world!')

# Read the file
with open('example_file.txt', 'r') as file:
    content = file.read()
    print(content)

# Change back to the parent directory
os.chdir('..')

# Remove the file and directory
os.remove('example_dir/example_file.txt')
os.rmdir('example_dir')

Conclusion

The os package in Python is a powerful tool for interacting with the operating system. It provides functionalities for directory and file operations, environment variables, and path manipulations. Understanding and utilizing these functions can significantly enhance your ability to manage files and directories programmatically.