1#the os module provides an operating system interface from Python
2import os
3#prints the name of the operating system
4print(os.name)
5#prints the absolute path for the module
6print(os.getcwd())
1# Delete everything reachable from the directory named in "top",
2# assuming there are no symbolic links.
3# CAUTION: This is dangerous! For example, if top == '/', it
4# could delete all your disk files.
5import os
6for root, dirs, files in os.walk(top, topdown=False):
7 for name in files:
8 os.remove(os.path.join(root, name))
9 for name in dirs:
10 os.rmdir(os.path.join(root, name))
11
1The OS module in Python provides a way of using operating system dependent
2functionality.
3
4The functions that the OS module provides allows you to interface with the
5underlying operating system that Python is running on – be that Windows, Mac or
6Linux.
7
8You can find important information about your location or about the process.
9
10In this post I will show some of these functions.
1# Import the built-in os module
2import os
3# os.system() function
4os.system('echo Hello world!')
1>>> import os
2>>> os.getcwd() # Return the current working directory
3'C:\\Python39'
4>>> os.chdir('/server/accesslogs') # Change current working directory
5>>> os.system('mkdir today') # Run the command mkdir in the system shell
60
7