w3resource

Python Operating System Services - Exercises, Practice, Solution


This resource offers a total of 90 Python OS Services problems for practice. It includes 18 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. OS Info and Directory Listing

Write a Python program to get the name of the operating system (Platform independent), information of the current operating system, current working directory, print files and directories in the current directory, and raise errors if the path or file name is invalid.

Click me to see the sample solution


2. List Directories and Files

Write a Python program to list only directories, files and all directories, files in a specified path.

Click me to see the sample solution


3. Directory Scanner

Write a Python program to scan a specified directory and identify the subdirectories and files.

Click me to see the sample solution


4. Path Access Checker

Write a Python program to check access to a specified path. Test the existence, readability, writability and executability of the specified path.

Click me to see the sample solution


5. File Metadata Fetcher

Write a Python program to get the size, permissions, owner, device, created, last modified and last accessed date and time of a specified path.

Click me to see the sample solution


6. Symbolic Link Creator

Write a Python program to create a symbolic link and read it to determine the original file pointed to by the link.

Click me to see the sample solution


7. File Creation and Renaming

Write a Python program to create a file and write some text and rename the file name.

Click me to see the sample solution


8. Process and User ID Operations

Write a Python program to find the parent's process ID, real user ID of the current process and change the real user ID.

Click me to see the sample solution


9. Change Directory Upwards

Write a Python program to retrieve the current working directory and change the dir (move up one).

Click me to see the sample solution


10. Environment Variables Access

Write a Python program to access environment variables and the value of the environment variable.

Click me to see the sample solution


11. Iterate Over Root Path

Write a Python program to iterate over a root level path and print all its sub-directories and files, as well as loop over specified dirs and files.

Click me to see the sample solution


12. Path Existence and Split

Write a Python program to test whether a given path exists or not. Find the filename and directory portion of the path if it exists.

Click me to see the sample solution


13. Join and Split Path Components

Write a Python program to join one or more path components together and split a given path into directories and files.

Click me to see the sample solution


14. Alter File Owner and Group

Write a Python program to alter the owner and the group ID of a specified file.

Click me to see the sample solution


15. File Mode Information

Write a Python program to get information about the file pertaining to file mode. Print the information - ID of device containing file, inode number, protection, number of hard links, user ID of owner, group ID of owner, total size (in bytes), time of last access, time of last modification and time of last status change.

Click me to see the sample solution


16. Buffer Write and Discard

Write a Python program to write a string to a buffer and retrieve the value written, at the end discard buffer memory.

Click me to see the sample solution


17. Run OS Command

Write a Python program to run an operating system command using the OS module.

Click me to see the sample solution


18. Replace Current Process

Write a Python program to start a new process replacing the current process.

Click me to see the sample solution


Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.