modules
31 July 2025 09:18 PM
Python os Module – Quick Revision Notes
1. Getting Current Directory
○ [Link]() → Returns the current working directory.
2. Listing Files and Folders
○ [Link](path) → Returns a list of files and directories in the given path.
○ Example: [Link]("my_folder")
3. Creating Directories
○ [Link]("folder_name") → Creates a single-level directory.
○ [Link]("folder/subfolder") → Creates multi-level directories.
4. Deleting Directories
○ [Link]("folder_name") → Removes a single-level empty directory.
5. Renaming Files or Folders
○ [Link]("old_name", "new_name")
6. Path Handling
○ [Link](part1, part2, ...) → Joins parts into a valid path string (cross-platform safe).
○ [Link]("filename") → Returns the absolute path.
7. Checking File or Directory Existence
○ [Link](path) → True if the path exists.
○ [Link](path) → True if it's a file.
○ [Link](path) → True if it's a directory.
8. Looping Through Files in a Folder
folder = "my_folder"
for file in [Link](folder):
full_path = [Link](folder, file)
if [Link](full_path):
print("File:", full_path)
9. Moving Files (Using shutil)
○ [Link](src, dst) → Safer for moving files, especially across different drives.
○ [Link]() can also move files if on same disk.
10. Common Use-Cases
• Automating folder cleanup
• Renaming/moving backups
• Building file paths dynamically
• Organizing data in folders
Tip: Always use [Link]() to avoid hardcoding slashes (/ or \).
You're now ready to use os for real-world file operations!
Python sys Module – Quick Revision Notes
The sys module provides access to system-specific parameters and functions. It's especially useful
for command-line scripting and interacting with the Python runtime.
Sys modules
1. What is [Link]?
• It is a list in Python that contains all command-line arguments passed to the script.
• [Link][0] is always the name of the script file.
• Arguments passed after the script name are stored as strings in the list. Example: If the script
is run as python my_script.py anu 22, then:
○ [Link] → ['my_script.py', 'anu', '22']
○ [Link][1] → 'anu'
○ [Link][2] → '22'
python important modules Page 1
○ [Link][2] → '22'
a=int(input([Link][0])
2. What is [Link]()?
• It is used to exit the program at any point.
• You can pass a value: 0 for normal exit, any other number for abnormal termination. Example:
If len([Link]) < 3, you can print a message and call [Link](1) to stop the program.
3. What is [Link] and sys.version_info?
• [Link] returns the version of Python being used as a string.
• sys.version_info returns version details in a tuple format like major, minor, micro, etc.
4. What is [Link]?
• It returns the absolute path to the Python interpreter binary currently executing the script.
Example: It might return a path like C:\Python311\[Link]
5. What is [Link]?
• It tells which OS platform you are using. Example: It returns 'win32' on Windows, 'linux' on
Linux, and so on.
6. What is [Link]?
• It is a list of directories that Python searches for modules during import.
• You can add paths using [Link]('folder_name') to make Python recognize custom
module folders.
Summary:
• [Link] → command-line arguments list
• [Link]() → manually exit program
• [Link] and sys.version_info → Python version details
• [Link] → interpreter path
• [Link] → OS type
• [Link] → module search paths
Useful for scripting, system details, and managing Python environments.
python important modules Page 2