IMPORTANCE OF USING OS AND SYS MODULE

Sulochana Kamshetty

8 months ago

OS AND SYS MODULE
OS AND SYS MODULE
Welcome to the new concept understanding about os  & sys module.
Let's understand these two important concepts used in our python programming language.
I find both the os and sys modules to be incredibly useful to me for the purposes of convenience and generality.
Main uses of os and sys
os.module plays a vital role in our python programming language since the changing of directory of python sheet helps us to work more feasibly so need to set with os.chdir("the current directory where we want to store the information with") this code helps to give the access to work from current directory to present set directory
Os.name: this function gives the name of the operating system dependent module module imported the following names have currently been registered: ‘posix’, ‘nt’, ‘os2’, ‘ce’, ‘java’, & ‘riscos’
Import os

Print(os.name)
Output :- nt
Note: it may give different outputs for different interpreters such as ‘posix’, ‘nt’, ‘ce’ when you run the code here its ‘nt’ 

Let us execute using os  module with all possibilities like as follows:
Main File Object Manipulation
  • os.popen(): 
  • os.open()
  • os.close()
  • os.popen() 
This method opens a pipe to or from command. The return value can be read or written depending on whether mode is ‘r’ or ‘w’.
Syntax:
 os.popen(command[, mode[, bufsize]])
Important note:-
if nothing provided by default 'r  mode is taken and Parameters mode & buf size are not necessary parameters, 
fd = "AIML.txt"

  popen() is similar to open() 

import os

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("insideaiml") 
Output:
Hello
Note: Output for popen() will not be shown, there would be direct changes into the file.
Now lets understand what is this os.close in python
os.close():- to open any file we use open() but if the close file is used as a descriptor we use close() to close the file or os.close() is used to close any file if we are trying a file to open with open(), using os.close() then it will throw an following error. because the file is operated with the help of open() command and stored the code for that.. 
import os 

fd = "AIML.txt"

file = open(fd, 'r') 

text = file.read() 

print(text) 

os.close(file) 

Output:

Traceback (most recent call last):

  File "C:\Users\system\Desktop\insideaiml
content.py", line 6, in 

    os.close(file)
TypeError: an integer is required (got type _io.TextIOWrapper)
Note:- if we have any non-existing of file or permission privilege then this may not throw the same error.
ABOUT os.rename():   Any file which is already created can be renamed using os.rename() to the existed file to old.txt---> to new.txt file using a simple command from the python programming language and also this is only possible when the user has sufficient entitlement to change the file 
import os 

fd = "AIML.txt"

os.rename(fd, insideaiml.txt') 

Output: new text document with insideaiml will be created in place of aiml.txt
Traceback (most recent call last):
  File "C:\Users\smithika\Desktop\insideaiml.
contentpy", line 3, in 

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

FileNotFoundError: [WinError 2] The system cannot find the

file specified: AIML.txt' -> 'New.txt'
Understanding the Output: A file name “AIML.txt” exists, thus when os.rename() is used the first time, the file gets renamed. Upon calling the function os.rename() second time, file “New.txt” exists and not “AIML.txt”
thus Python throws FileNotFoundError..
 the os and sys modules gives the access to deal with many tools like filenames, path, and directories.
the os.sys and os.path are two submodules which are actively used to the system directories
os.path split is used on different support platform like unix, windows, mac os, etc 

What is sys module? 

lets understand what is sys module this is used for, command line inputs to the program this module is been used in many life applications.
import sys
sys.argv : A list of
strings which is the command line used to execute the script separated by
spaces.

sys.exit() : Exit Python
cleanly

sys.version : The Python
version string
Here, sys.arg represents the list of command-line arguments, with the name of the code as the first element. If I’ve put in the correct parameters, then the program should proceed smoothly with sys.argv[1] and sys.argv[2] seamlessly incorporated.
A better way is to make another Python script and use os.system to call kmeans_clustering as many times as I want. And this is as easy as changing the input to os.system. It takes in a string, so I would set a for loop that would create its unique string which would then act as input to the command line.
Now let us understand about coding part
 import sys

 x = 42

def my_display(x):

 sys.displayhook = my_display

print x
output = 42

We can see from this example that the standard behavior of the print() function will not be changed.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review