Python os module tutorial

Python os module tutorial : OS module in python provides functions for interacting with the operating system. OS, comes under Python’s standard utility modules. It allows using in a variety of systems and allowing them to function with other systems.The module is the separate independent unit of hardware/software. It allows using in a variety of systems and allowing them to function with other systems. The characteristic of the module include portability which allows using a variety of systems and functions with other systems. The module used in interacting operating systems under the python utility module. The *os* and *os.path* will include a function to interact with the file system. The getcwd () function return current directory and listdir () return content of directory. So they will mix both the directories and files. The importing of the statement is used to import library into code and use the functionalities.
python os module tutorial
Python os module tutorial

Python Os functions are as follows:-

Executing the shell command by os.system ().
Getting the user environment by os.envion ().
Return the real group id of the current process by using os.getgid ()
Return the real process id of the current process by using os.getpid ()
Setting the current numeric umask and then return umask by os.umask (mask).
Then we return information to identify the current operating system by os.uname ().
Change the root directory of the current process to the path by os.chroot (path)
Return list of entries in a directory by the path as os.listdir (path).
Then we create the directory named path with numeric mode as os.mkdir (path).
We remove (delete) file path by os.removedirs (path).
Rename the file src to dst by os.rename (src, dst).

Example:-

import os
print (os.name)
Output:-
Posix
It gives the output on different interpreters as posix after running the code.

1. Os.getcwd ():-

It returns the current working directory of files which are used to execute the code.
Example:-
import os
print(os.getcwd ())
Output:-
C:\Users\GFG\Desktop\ModuleOS

2. Os.error:-

The function will raise OSerror of invalid file name and path that will have correct type.
It is not accepted by the operating system.
The os.error is built-in OSerror exception.

 Example:-
import os
try:
filename=’GFG.txt’
f=open (filename,’rU’)
text=f.read ()
f.close ()
except IOError:
print (‘Problem reading:’+filename)
Output:-
Problem reading: GFG.txt

3. Os.popen ():-

It will open the pipe from command and return value depending in ‘r’ or ‘w’.
Syntax:-
                      Os.popen (command [, model [, bufsize]])
Example:-
import os
fd=”GFG.txt”
file=open (fd,’w’)
file. write (“Hello”)
file. close ()
file=open (fd,’r’)
text=file. read ()
print (text)
file=os.popen (fd,’w’)
file. write (“Hello”)
Output:-
 Hello

4. Os.close ():-

The file is opened using the open () and closed by the close ().if the file is opened by os.popen () can be closed by os.close () or close ().
Example:-
import os
fd=”GFG.txt”
file=open (fd,’r’)
text=file. read ()
print (text)
os.close (file)
Output:-
           Error

5. Os.rename:-

The old.txt is renamed to new.txt by using this function and the name will change if the file will exist and the user has permission to change.
Example:-
import os
fd=”GFG.txt”
os.rename (fd,’New.txt’)
os.rename (fd,’New.txt’)
Output:-
error

6. Os.getpid:-

It will return the process ID of the current process.
Example:-
import os
print (os.getpid ())
Output:-
17026

7. Os.sytem ():-

The system function will allow running command in the python scripy and running it in my shell.
Example:-
import os
newFile=os.system (‘users>app.txt’)
The new file created is called app.txt and keunal is return inside of the computer.

8. Os.access:-

They use uid/gid to test if the user will have access to the path.
Example:-
import os
import sys
path1=os.access (“python.txt, os.F_OK”)
print (“Exist path:’ path1)
Path2=os.access (“python.txt”,os.R_OK)
print (“It access to read the file:” path2)
 path3=os.access (“python.txt”, os.W_OK)
print (“It access to read the file:” path3)
 path4=os.access (“python.txt”, os.X_OK)
print (“check if file can be executed:” path4)
Output:-
Exist path: False
It access to read the file:False
It access to read the file:False
Check if path can be executed: False

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India