OS Module in Python: All You Need to Know

Published on Sep 25,2019 9.8K Views

OS Module in Python: All You Need to Know

edureka.co

Python is one of the most powerful programming languages available in the industry today. Thanks to its wide number of features and great versatility, a lot of complex programming objectives can be achieved in Python quite easily. In this article, we will discuss OS Module in Python in the following order:

 

What is the OS Module in Python?

The OS module in Python is a part of the standard library of the programming language. When imported, it lets the user interact with the native OS Python is currently running on. In simple terms, it provides an easy way for the user to interact with several os functions that come in handy in day to day programming.

The OS module and os.path modules are the same and can be easily imported from the standard library, at a moment’s notice.

 

Functions of the OS module

Now that you know the definition of the OS module, let us look at some of its functions.

import os 
print(os.name) 

Output:

posix

Note: The above program will give a different output depending upon the operating system you are currently using.

 

import os 
print(os.getcwd()) 
# To print absolute path on your system 
# os.path.abspath('.') 

# To print files and directories in the current directory 
# on your system 
# os.listdir('.')

Output:

C:UsersGFGDesktopModuleOS

Note: If you are using a GFG interpreter, then the directory used by default will be /root.

 

import os 
try: 
	# If the file does not exist, 
	# then it would throw an IOError 
	filename = 'GFG.txt'
	f = open(filename, 'rU') 
	text = f.read() 
	f.close() 

# Control jumps directly to here if 
#any of the above lines throws IOError.	 
except IOError: 

	# print(os.error) will <class 'OSError'> 
	print('Problem reading: ' + filename) 
	
# In any case, the code then continues with 
# the line after the try/except

Output:

Problem reading: GFG.txt

 

import os 
fd = "GFG.txt"

# popen() is similar to open() 
file = open(fd, 'w') 
file.write("Hello") 
file.close() 
file = open(fd, 'r') 
text = file.read() 
print(text) 

# popen() provides a pipe/gateway and accesses the file directly 
file = os.popen(fd, 'w') 
file.write("Hello") 
# File not closed, shown in the next function.

Output:

Hello

 

import os 
fd = "GFG.txt"
file = open(fd, 'r') 
text = file.read() 
print(text) 
os.close(file)

Output:

Traceback (most recent call last):

  File "C:UsersGFGDesktopGeeksForGeeksOSFile.py", line 6, in 

    os.close(file)

TypeError: an integer is required (got type _io.TextIOWrapper)

 

import os 
fd = "GFG.txt"
os.rename(fd,'New.txt') 
os.rename(fd,'New.txt')

Output:

Traceback (most recent call last):

  File "C:UsersGFGDesktopModuleOSGeeksForGeeksOSFile.py", line 3, in 

    os.rename(fd,'New.txt')

FileNotFoundError: [WinError 2] The system cannot find the

file specified: 'GFG.txt' -> 'New.txt'

The os module in Python can be used to access a lot of operating system functions. Now that you know its uses, we hope that you will make use of the same in your day to day programming.

With this, we come to an end of this OS Module in Python. I hope all your doubts about OS Module is cleared now.

To get in-depth knowledge on Python along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of “Membership Operators in Python” and we will get back to you.

BROWSE COURSES