w3resource

Python Operating System Services - Exercises, Practice, Solution

Python OS Services [18 exercises with solution]

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

1. 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. 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. Write a Python program to scan a specified directory and identify the subdirectories and files.
Click me to see the sample solution

4. 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. 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. 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. 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. 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. 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. Write a Python program to access environment variables and the value of the environment variable.
Click me to see the sample solution

11. 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. 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. 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. Write a Python program to alter the owner and the group ID of a specified file.
Click me to see the sample solution

15. 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. 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. Write a Python program to run an operating system command using the OS module.
Click me to see the sample solution

18. 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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/os/index.php