week 7

Week 7: Working with OS & Threads - 40 Python Snippets

This week covers system modules, threading, multiprocessing, logging, and static code analysis. Below are 40 Python snippets categorized into 4 topics (10 per topic).


1️⃣ sys, os, shutil, and subprocess (10 Snippets)

1. Getting Python Version using sys

import sys
print(sys.version)

2. Getting Command-Line Arguments using sys

import sys
print("Arguments passed:", sys.argv)

3. Listing Files in a Directory using os

import os
print(os.listdir("."))

4. Getting the Current Working Directory using os

import os
print(os.getcwd())

5. Creating a New Directory using os

6. Removing a File using os

7. Copying a File using shutil

8. Moving a File using shutil

9. Running a Shell Command using subprocess

10. Capturing Output from a Shell Command using subprocess


2️⃣ Multithreading and Multiprocessing (10 Snippets)

11. Creating a Simple Thread

12. Running Multiple Threads

13. Using a Lock in Threads

14. Creating a Process using multiprocessing

15. Running Multiple Processes

16. Using a Queue in Multiprocessing

17. Using a Pool of Processes

18. Using a Shared Variable in Multiprocessing

19. Using Manager for Shared Data

20. Killing a Process


3️⃣ Logging & Debugging with logging (10 Snippets)

21. Basic Logging Configuration

22. Writing Logs to a File

23. Logging Different Levels

24. Using Custom Log Format

25. Logging Exceptions

26. Creating a Custom Logger

27. Adding Multiple Handlers

28. Using Rotating File Handler

29. Disabling Logging for a Module

30. Using Logging in a Class


4️⃣ Pylint for Static Code Analysis (10 Snippets)

31. Installing Pylint

32. Running Pylint on a File

33. Checking a Python File for Issues

34. Ignoring Warnings in Pylint

35. Generating a Pylint Report


Last updated