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)