Skip to content

Commit b3a5374

Browse files
committed
python os module commit
1 parent ae68a78 commit b3a5374

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

os_py/os_module.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import os
2+
3+
# -------------------------------
4+
# 📂 1. DIRECTORY OPERATIONS
5+
# -------------------------------
6+
# print("Current Working Directory:", os.getcwd())
7+
8+
# # Create a new folder
9+
# os.mkdir("demo_folder")
10+
# print("\nAfter creating 'demo_folder':", os.listdir())
11+
12+
# # Rename folder
13+
# os.rename("demo_folder", "renamed_folder")
14+
# print("After renaming:", os.listdir())
15+
16+
# # Change directory
17+
# os.chdir("renamed_folder")
18+
# print("Now inside directory:", os.getcwd())
19+
20+
# # Go back to parent directory
21+
# os.chdir("..")
22+
23+
# # Remove directory
24+
# os.rmdir("renamed_folder")
25+
# print("After removing folder:", os.listdir())
26+
27+
# -------------------------------
28+
# 🧭 2. PATH HANDLING
29+
# -------------------------------
30+
# print("\n--- Path Operations ---")
31+
# path = os.getcwd()
32+
33+
# print("Absolute Path:", os.path.abspath(path))
34+
# print("Is Directory:", os.path.isdir(path))
35+
# print("Is File:", os.path.isfile(__file__))
36+
# print("File Name:", os.path.basename(__file__))
37+
# print("Directory Name:", os.path.dirname(__file__))
38+
# print("Path Exists:", os.path.exists(path))
39+
# print("Joined Path:", os.path.join(path, "sample.txt"))
40+
41+
# -------------------------------
42+
# 🌍 3. ENVIRONMENT VARIABLES
43+
# -------------------------------
44+
# print("\n--- Environment Variables ---")
45+
# print("OS Name:", os.name)
46+
# print("Home Directory:", os.getenv("HOME") or os.getenv("USERPROFILE"))
47+
# print("Python Path:", os.getenv("PATH").split(";")[0]) # Show first path
48+
49+
# # Setting and unsetting an environment variable
50+
# os.putenv("MY_VAR", "User_name")
51+
# print("MY_VAR (after putenv):", os.getenv("MY_VAR"))
52+
53+
# os.unsetenv("MY_VAR")
54+
# print("MY_VAR (after unsetenv):", os.getenv("MY_VAR"))
55+
56+
# -------------------------------
57+
# ⚙️ 4. SYSTEM COMMANDS
58+
# -------------------------------
59+
print("\n--- System Commands ---")
60+
# Example: list files using OS command
61+
os.system("dir" if os.name == "nt" else "ls")
62+
63+
# -------------------------------
64+
# 🧱 5. PROCESS INFORMATION
65+
# -------------------------------
66+
print("\n--- Process Info ---")
67+
print("Process ID:", os.getpid())
68+
print("CPU Count:", os.cpu_count())
69+
70+
# -------------------------------
71+
# 🔐 6. FILE ACCESS & PERMISSIONS
72+
# -------------------------------
73+
print("\n--- File Access & Permissions ---")
74+
filename = "testfile.txt"
75+
with open(filename, "a+") as f:
76+
f.write("This is a test file for os module demo.\n")
77+
78+
print("File Created:", filename)
79+
print("File Size:", os.path.getsize(filename), "bytes")
80+
print("Has Read Access:", os.access(filename, os.R_OK))
81+
print("Has Write Access:", os.access(filename, os.W_OK))
82+
83+
# -------------------------------
84+
# 🧩 7. CLEANUP
85+
# -------------------------------
86+
os.remove(filename)
87+
print("\nFile Removed:", filename)
88+
89+
# print("\n✅ All OS operations completed successfully!")

0 commit comments

Comments
 (0)