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

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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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. Go to the editor
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). Go to the editor
Click me to see the sample solution

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

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

17. Write a Python program to run an operating system command using the OS module. Go to the editor
Click me to see the sample solution

18. Write a Python program to start a new process replacing the current process. Go to the editor
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.

Python: Tips of the Day

F strings:

It is a common practice to add variables inside strings. F strings are by far the coolest way of doing it. To appreciate the f strings more, let's first perform the operation with the format function.

name = 'Owen'
age = 25
print("{} is {} years old".format(name, age))

Output:

Owen is 25 years old

We specify the variables that go inside the curly braces by using the format function at the end. F strings allow for specifying the variables inside the string.

name = 'Owen'
age = 25
print(f"{name} is {age} years old")

Output:

Owen is 25 years old

F strings are easier to follow and type. Moreover, they make the code more readable.

A, B, C = {2, 4, 6}
print(A, B, C)
A, B, C = ['p', 'q', 'r']
print(A, B, C)

Output:

2 4 6
p q r