Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions os_py/os_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import os

Check failure on line 1 in os_py/os_module.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

os_py/os_module.py:1:1: INP001 File `os_py/os_module.py` is part of an implicit namespace package. Add an `__init__.py`.

# -------------------------------
# 📂 1. DIRECTORY OPERATIONS
# -------------------------------
# print("Current Working Directory:", os.getcwd())

# # Create a new folder
# os.mkdir("demo_folder")
# print("\nAfter creating 'demo_folder':", os.listdir())

# # Rename folder
# os.rename("demo_folder", "renamed_folder")
# print("After renaming:", os.listdir())

# # Change directory
# os.chdir("renamed_folder")
# print("Now inside directory:", os.getcwd())

# # Go back to parent directory
# os.chdir("..")

# # Remove directory
# os.rmdir("renamed_folder")
# print("After removing folder:", os.listdir())

# -------------------------------
# 🧭 2. PATH HANDLING
# -------------------------------
# print("\n--- Path Operations ---")
# path = os.getcwd()

# print("Absolute Path:", os.path.abspath(path))
# print("Is Directory:", os.path.isdir(path))
# print("Is File:", os.path.isfile(__file__))
# print("File Name:", os.path.basename(__file__))
# print("Directory Name:", os.path.dirname(__file__))
# print("Path Exists:", os.path.exists(path))
# print("Joined Path:", os.path.join(path, "sample.txt"))

# -------------------------------
# 🌍 3. ENVIRONMENT VARIABLES
# -------------------------------
# print("\n--- Environment Variables ---")
# print("OS Name:", os.name)
# print("Home Directory:", os.getenv("HOME") or os.getenv("USERPROFILE"))
# print("Python Path:", os.getenv("PATH").split(";")[0]) # Show first path

# # Setting and unsetting an environment variable
# os.putenv("MY_VAR", "User_name")
# print("MY_VAR (after putenv):", os.getenv("MY_VAR"))

# os.unsetenv("MY_VAR")
# print("MY_VAR (after unsetenv):", os.getenv("MY_VAR"))

# -------------------------------
# ⚙️ 4. SYSTEM COMMANDS
# -------------------------------
print("\n--- System Commands ---")
# Example: list files using OS command
os.system("dir" if os.name == "nt" else "ls")

Check failure on line 61 in os_py/os_module.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (S605)

os_py/os_module.py:61:1: S605 Starting a process with a shell, possible injection detected

# -------------------------------
# 🧱 5. PROCESS INFORMATION
# -------------------------------
print("\n--- Process Info ---")
print("Process ID:", os.getpid())
print("CPU Count:", os.cpu_count())

# -------------------------------
# 🔐 6. FILE ACCESS & PERMISSIONS
# -------------------------------
print("\n--- File Access & Permissions ---")
filename = "testfile.txt"
with open(filename, "a+") as f:
f.write("This is a test file for os module demo.\n")

print("File Created:", filename)
print("File Size:", os.path.getsize(filename), "bytes")
print("Has Read Access:", os.access(filename, os.R_OK))
print("Has Write Access:", os.access(filename, os.W_OK))

# -------------------------------
# 🧩 7. CLEANUP
# -------------------------------
os.remove(filename)
print("\nFile Removed:", filename)

# print("\n✅ All OS operations completed successfully!")
Loading