Sitemap
SHUBHAM INGOLE

SHUBHAM INGOLE

Data Analyst

What is Os Module

1 min readJan 5, 2023
Press enter or click to view image in full size

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.

  1. Get the name of the current operating system:
import os
os_name = os.name
print(os_name)

2. Get the current working directory:

import os
cwd = os.getcwd()
print(cwd)

3. List the files in a directory:

import os
files = os.listdir('/path/to/directory')
print(files)

4. Change the current working directory:

import os
os.chdir('/path/to/new/directory')

5. Create a new directory:

import os
os.mkdir('/path/to/new/directory')

6. Remove a directory

import os
os.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

--

--