Python OS Module Tutorial with Examples

The python os module provides different operating systems related to functionalities. For example, if you want to list current operating system variables you can use the os.environ attribute which will print all operating system variables which may change according to the operating system type. The os module is named after the operating system where taking the first letters of it.

What Is OS Module In Python?

The Python OS module is a module to provide miscellaneous operating system methods and interfaces. The OS module is a built-in module that is provided by Python natively and there is no need to install an extra module. The Python OS module provides features about environment variables, path, user, process id, umask, file, random values, etc.

Is OS a Standard Python Library?

The Python OS is a standard Python library this means there is no need to install extra modules or packages.

How To Open/Import OS Module In Python?

As a standard Python module the os module can be easily opened or imported by using the import . Also specific method or submodule of the os module can be imported like below.

#Import OS module
import os

#Import All Methods and Attributes of the OS Module
import os.*

os.name – Print Operating System Name

The os module provides the name attribute via os.name which will provide the name o the current operating system. The output will be POSIX where the current operating system is POSIX type which is generally used for Unix and Linux operating systems. There are also other operating system types like nt (For Windows operating systems), os2, ce, java and riscos.

import os

print(os.name)

os.getcwd() – Print Current Working Directory or Path

While working with files, running commands there is a current working directory. The Current Working Directory or CWD can be printed with the getcwd() method like below.

import os

print(os.getcwd())

The current working directory will be the user home directory for Linux like /home/ismail and for Windows C:\Users\─░smail .

os.environ – List Environment Variables

Operating systems like Linux and Windows has environment variables in order to store some basic information. The os module provides the environ attribute in order to list environment variables.

import os

print(os.environ)
os.environ – List Environment Variables

os.chdir() – Change Current Working Directory

The chdir() method can be used to change the current working directory or path. The dhcidr() method accept the path as parameter where this can be an absolute or relative path.

import os

#Change Directory For Windows
os.chdir("C:\\Users")

#Change Directory For Linux
os.chdir("//home//ismail")

#Change Directory With Relative Path For Linux and Windows
os.chdir("temp")

os.mkdir() – Create Directory

The OS module provides the mkdir() method in order to create new directory. The directory name provided as parameter where the given name will be created in the current working directory. Also the absolute path and name of the directory can be used too.

import os

#Create Directory For Windows
os.mkdir("C:\\Users\\ismail\\deneme")

#Create Directory For Linux
os.mkdir("//home//ismail//deneme")

#Create Directory With Relative Path For Linux and Windows
os.mkdir("temp")

os.listdir() – List All Files and Folders

The listdir() method is used to list all files and folders for the current working directory if no parameter is specified. Also a path can be provided the list the given path content file and folders.

import os

#Change Directory For Windows
os.lsdir("C:\\Users\\ismail\\deneme")

#Change Directory For Linux
os.lsdir("//home//ismail//deneme")

#Change Directory With Relative Path For Linux and Windows
os.lsdir("temp")

os.error – Raise OSError Exceptions

Python is a modern programming language which also provides different type of exceptions. The OS module also provides OSError exceptions which are related with the invalid file names, paths or error to open files etc.

import os

try:
   filename = "test.txt"
   f = open(filename,'r')
   text = f.read()
   f.lose()

except IOError:
   print("The exception is" + file)

os.popen() – Run Operating System Command

The OS module provides the popen() method which will run an operating system command like executing it in the command line. This is called an open pipe where the command is executed and the output is written into the specified file.

import os

#This will run the ls command in read mode
output = os.popen("ls","r",1)

os.close() – Close File

The os module also provides the close() method which is used to close allready opened files.

import os

filename = "text.txt"

f = open(filename,'r')

content = file.read()

print(content)

os.close(f)

os.rename() – Rename File

The OS module also provides the rename() method which will simply rename the specified file into a new file name. But also the user has the required privileges in order to rename the given file into a new file name. We can provide the new and old file names as variable or string literal like below.

import os

filename = "test.txt"

os.rename(filename , "newtest.txt")

os.rename( "newtest.txt" , "morenewtest.txt")

Leave a Comment