0% found this document useful (0 votes)
3 views1 page

OS Module Methods

The document outlines several methods from the OS module in Python, including os.chdir for changing the current working directory, os.rmdir for removing empty directories, os.walk for generating file and directory names in a tree structure, and os.listdir for listing files and directories in a specified path. Each method is accompanied by a brief example demonstrating its usage. These methods are essential for file and directory manipulation in Python.

Uploaded by

charanbhat5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

OS Module Methods

The document outlines several methods from the OS module in Python, including os.chdir for changing the current working directory, os.rmdir for removing empty directories, os.walk for generating file and directory names in a tree structure, and os.listdir for listing files and directories in a specified path. Each method is accompanied by a brief example demonstrating its usage. These methods are essential for file and directory manipulation in Python.

Uploaded by

charanbhat5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

OS Module Methods in Python

1. os.chdir(path)

Changes the current working directory to the given path.


Example:
import os
print("Before:", os.getcwd())
os.chdir("C:/Users")
print("After:", os.getcwd())

2. os.rmdir(path)

Removes (deletes) an empty directory. The directory must be empty.


Example:
import os
os.mkdir("test_folder")
os.rmdir("test_folder")

3. os.walk(top)

Generates the file names and directory names in a directory tree.


Example:
import os
for dirpath, dirnames, filenames in os.walk("C:/Users/Example"):
print("Directory:", dirpath)
print("Subdirectories:", dirnames)
print("Files:", filenames)

4. os.listdir(path)

Returns a list of all files and directories in the specified path.


Example:
import os
items = os.listdir("C:/Users")
print(items)

You might also like