What is Os Module
The os module in Python provides functions for interacting with the operating system. For example, you can use the os.getcwd() function to get the current working directory, the os.listdir() function to list the files in a directory, and the os.rename() function to rename a file.
Get SHUBHAM INGOLE’s stories in your inbox
Join Medium for free to get updates from this writer.
Here is an example of how you might use the os module to list the files in the current working directory:
import os
files = os.listdir()
print(files)You can also use the os module to set and get environment variables. For example, the following code gets the value of the HOME environment variable:
import os
home_dir = os.environ['HOME']
print(home_dir)For more information about the os module, you can refer to the Python documentation.
- Get the name of the current operating system:
import osos_name = os.name
print(os_name)2. Get the current working directory:
import oscwd = os.getcwd()
print(cwd)3. List the files in a directory:
import osfiles = os.listdir('/path/to/directory')
print(files)4. Change the current working directory:
import osos.chdir('/path/to/new/directory')5. Create a new directory:
import osos.mkdir('/path/to/new/directory')6. Remove a directory
import osos.rmdir('/path/to/directory')For more information, you can refer to the documentation for the os module: https://docs.python.org/3/library/os.html







