Qayyum Siddiqui logo
Qayyum Siddiqui
python

Most used Python os Module Functions

Most used Python os Module Functions
0 views
8 min read
#python

Python's OS module offers ways to communicate with the operating system. Python's standard utility modules include OS. This module offers an approach to use operating system-dependent functionality that is portable. Numerous functions for interacting with the file system are included in the os and os.path modules. the os module in Python!

It's like the Swiss Army knife for your operating system.

Here's a quick rundown:

  • File and Directory Operations: You can create, remove, rename files and directories. Think of it as having a digital janitor at your service.

  • Environment Variables: Want to peek at or change environment variables? os is your spy.

  • Path Operations: Manipulating paths? os.path is your GPS in the file system jungle.

  • Process Management: Need to run other programs or check if they're running? os can be your personal assistant.

  • System Calls: Sometimes you need to dive deep into system calls. os gives you that scuba gear.

To print the contents of a directory using Python's os module, we can use the os.listdir() function. Here's a step-by-step approach to create this program:

  1. Import the os module: This module provides a way to interact with the operating system, including file and directory operations.

  2. Specify the directory path: We'll need to define which directory we want to list.

  3. Use os.listdir(): This function returns a list of entries (files and directories) in the given path.

  4. Print the contents: Iterate over the list and print each item.

Here's the Python code to accomplish this:

import os

#Define the path to the directory you want to list
#For this example, we'll use the current directory, which can be obtained with os.getcwd()
directory_path = os.getcwd()

#Get the list of entries in the directory
directory_contents = os.listdir(directory_path)

#Print each item in the directory
print(f"Contents of {directory_path}:")
for item in directory_contents:
    print(item)

Explanation:

  • os.getcwd(): This gets the current working directory. You can replace this with any path you want to list, like "/home/user/documents" on Unix-like systems or "C:\\Users\\Username\\Documents" on Windows.
  • os.listdir(directory_path): This returns a list of all entries in the specified directory.
  • The for loop iterates over each item in the list and prints it.

Notes:

  • This script will list both files and directories within the specified path.
  • If you want to distinguish between files and directories, you could use os.path.isdir() or os.path.isfile() within the loop to check the nature of each item.
  • Remember, os.listdir() does not return the full path of the items, just their names. If you need the full path, you would concatenate directory_path with each item.

This code will work on most systems where Python is installed, as it uses the standard os module which abstracts away many system-specific differences.

Most commonly used functions of OS module:

Here's a list of some important functions from Python's os module, which are useful for various file and directory operations:

  1. os.listdir(path) - Returns a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.
import os

directory_path = "./"
directory_contents = os.listdir(directory_path)


print(f"Contents of {directory_path}:")
for item in directory_contents:
    print(item)

#output
.venv
chapter1
chapter2
chapter3
chapter4
  1. os.path.exists(path) - Returns True if path refers to an existing path or file.
import os

directory_path = "./chapter1"    
file_present = os.path.exists("./chapter1")
print(file_present)

#output
True
  1. os.path.isfile(path) - Returns True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.
import os
print(os.getcwd())
print(os.path.isfile("./problem1.py"))

#output
G:\machine-learning-python\python\chapter1
True
  1. os.path.isdir(path) - Returns True if path is an existing directory. This follows symbolic links, so both islink() and isdir() can be true for the same path.
import os

print(os.path.isdir('../chapter1')) #True
print(os.path.isdir('./chapter1'))  #False
  1. os.path.join(path, *paths) - Joins one or more path components intelligently. This is useful for building paths in a cross-platform way.
import os

print(os.path.join('..\chapter2' , 'problem1.py')) #..\chapter2\problem1.py
print(os.path.join('..\python' , 'chapter','problem1.py')) #..\python\chapter\problem1.py
  1. os.path.basename(path) - Returns the final component of a pathname, which is the opposite of dirname.
import os

print(os.path.basename('../chapter1')) #chapter1
print(os.path.basename('./problem2.py')) #problem2.py
  1. os.path.dirname(path) - Returns the directory name of pathname path. This does not follow symbolic links.
import os

print(os.path.dirname('./python/chapter1')) #./python
print(os.path.dirname('chapter/problem2.py')) #chapter
  1. os.path.abspath(path) - Returns the absolute path of path.
import os

print(os.path.abspath('./problem1.py')) #G:\machine-learning-python\python\chapter1\problem1.py
  1. os.path.relpath(path, start=current_working_directory) - Returns a relative filepath to path either from the current directory or from an optional start directory.
import os

print(os.path.relpath('problem1.py', start="G:\machine-learning-python\python")) 
print(os.path.relpath('../chapter1'))
print(os.path.relpath('../chapter1' , start="G:\machine-learning-python"))

#output
chapter1\problem1.py
. (relative path)
python\chapter1
  1. os.getcwd() - Returns the current working directory.
import os

print(os.getcwd()) 

#output
G:\machine-learning-python\python\chapter1
  1. os.chdir(path) - Changes the current working directory to path.
import os

print(f"current working directory before changing: {os.getcwd()}")
os.chdir("../chapter2") #changing cwd to chapter2
print(f"current working directory After changing: {os.getcwd()}")

#output
current working directory before changing: G:\machine-learning-python\python\chapter1
current working directory After changing: G:\machine-learning-python\python\chapter2
  1. os.makedirs(name[, mode=0o777, exist_ok=False]) - Recursively create a directory and all its missing parents.
import os

os.makedirs("./chapter3")

#output
FileExistsError: [WinError 183] Cannot create a file when that file already exists: './chapter3'


#first we will check whether "chapter5" directory exists or not
print(os.path.exists("./chapter5")) 
#then we will create a dir with name chapter5
os.makedirs("./chapter5")
#check if dir "chapter5" exists
print(os.path.exists("./chapter5"))

#output
False (as there's no chapter5 directory)
True (we created chapter 5 directory using os.makedirs(path))
  1. os.removedirs(name) - Remove directories recursively. Works the opposite way of makedirs().
import os

#removes chapter5 dir we just created
os.removedirs('./chapter5')
print(os.path.exists('./chapter5'))

#output
false
  1. os.rename(src, dst) - Rename the file or directory src to dst.
import os

#check the chapter3 exists
print(os.path.exists("../chapter3"))  #Output: True

#rename the directory chapter3 to chapter-3
os.rename('../chapter3', '../chapter-3')

#check dir chapter3 existence
print(os.path.exists("../chapter3")) #output: False

#check dir chapter-3 existence
print(os.path.exists("../chapter-3")) #output: True
  1. os.remove(path) - Remove (delete) the file path. If path is a directory, use os.rmdir() or shutil.rmtree().
import os

#check if problem1.py file exists or not
print(os.path.isfile("./problem1.py")) #output: True

#remove the problem1.py file
os.remove("./problem1.py")

#check again if problem1.py file exists or not
print(os.path.isfile("./problem1.py")) #output: False
  1. os.rmdir(path) - Remove (delete) the directory path only if it's empty.
import os

#check if problem1.py file exists or not
print(os.path.isdir("../chapter5")) #output: True

#remove the chapter5 directory
os.rmdir("../chapter5")

#check again if chapter5 dir exists or not
print(os.path.isdir("../chapter5")) #output: False
  1. os.walk(top[, topdown=True, onerror=None, followlinks=False]) - Generate file names in a directory tree, by walking either top-down or bottom-up.
import os

for dirpath, dirnames, filenames in os.walk('.'):
    print(f'Current Path: {dirpath}')
    print(f'Directories: {dirnames}')
    print(f'Files: {filenames}')
    
    
#output
Current Path: .
Directories: ['.ipynb_checkpoints']
Files: ['problem1.py', 'problem2.py']
Current Path: .\.ipynb_checkpoints
Directories: []
Files: ['problem1-checkpoint.py', 'Untitled-checkpoint.ipynb']

Use Case: You want to find all .txt files in a directory tree? os.walk() is your detective.

import os

txt_files = []
for dirpath, _, filenames in os.walk('.'):
    for name in filenames:
        if name.endswith('.txt'):
            txt_files.append(os.path.join(dirpath, name))
            
            
print(txt_files) #output: ['.\\python.txt']
  1. os.environ - A dictionary representing the string environment. For example, os.environ['PATH'] gives the PATH environment variable.
import os

#Setting an environment variable
os.environ['MY_SECRET_KEY'] = 'super_secret_value'

#Reading an environment variable
print(f"MY_SECRET_KEY is: {os.environ.get('MY_SECRET_KEY')}")

#Checking if an environment variable exists
if 'MY_SECRET_KEY' in os.environ:
    print("MY_SECRET_KEY exists!")
else:
    print("MY_SECRET_KEY does not exist!")

#Listing all environment variables
print("\nAll Environment Variables:")
for key, value in os.environ.items():
    print(f"{key}: {value}")

#Deleting an environment variable (only for this process)
del os.environ['MY_SECRET_KEY']

#Try to access it after deletion
try:
    print(f"MY_SECRET_KEY after deletion: {os.environ['MY_SECRET_KEY']}")
except KeyError:
    print("MY_SECRET_KEY has been deleted!")

#Setting a path-like environment variable
os.environ['PATH'] += os.pathsep + '/usr/local/bin'

#Note: Changes to PATH here won't affect the system PATH, only this process.

#output
MY_SECRET_KEY is: super_secret_value
MY_SECRET_KEY exists!

All Environment Variables:
ALLUSERSPROFILE: C:\ProgramData
APPDATA: C:\Users\pc\AppData\Roaming
CHOCOLATEYINSTALL: C:\ProgramData\chocolatey
CHOCOLATEYLASTPATHUPDATE: 
CHROME_CRASHPAD_PIPE_NAME: \\.\pipe\
COMMONPROGRAMFILES: C:\Program Files\Common Files
COMMONPROGRAMFILES(X86): C:\Program Files (x86)\Common Files
COMMONPROGRAMW6432: C:\Program Files\Common Files
.
.
.
.
  1. os.path.getsize(filename) - Return the size, in bytes, of filename.
import os

print(os.path.getsize('./problem2.py'))

#output
54
  1. os.path.getmtime(filename) - Return the last modification time of filename, as a number of seconds since the epoch.

  2. os.path.getctime(filename) - Return the metadata change time of filename, as a number of seconds since the epoch.

  3. os.path.getatime(filename) - Return the last access time of filename

import os

print(os.path.getctime('./problem2.py')) #output : 1724516587.2573276
print(os.path.getctime('./problem2.py')) #output : 1724516587.2573276
print(os.path.getatime('./problem2.py')) #output : 1726065157.5593412
  1. os.path.realpath(path) - Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path.
import os

print(os.path.realpath('./problem1.py'))

#output
G:\machine-learning-python\python\chapter1\problem1.py

These functions cover a wide range of operations from directory navigation, file and directory creation/deletion, to path manipulation and environment interaction. They're essential for any Python script that needs to interact with the filesystem. Remember, for more complex file operations or for copying/moving files, you might also want to look into the shutil module.