Python OS module with examples

Below are some of the functions in the OS module:

1. os.name: This function gives the name of the imported dependent operating system module. The names currently registered are: “posix”, “nt”, “os2”, “ce”, “java” and “riscos”

import os

print (os.name)

Output:

 posix  

Note: it may give different output on different interpreters like "posix" when you run the code here.

2. os.getcwd (): The os.getcwd () function, which returns the current working directory (CWD) of the file used to execute the code, can vary from system to system.

import os

print (os.getcwd ())

# To print the absolute path on your system
# os.path.abspath (& # 39;. & # 39;)

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

Output:

 C: UsersGFGDesktopModuleOS 

Note. In the case of the GFG interpreter, the / root directory is used.

3. os.error: All functions in this module raise OSError for invalid or unreachable file names and paths or other arguments that are of the correct type but are not accepted by the operating system. os.error — it is an alias for the inline OSError exception.

< / tr>

import os

try :

# If the file does not exist,

  # then this will throw an IOError

filename = `GFG.txt`

  f = open (filename, `rU` )

text = f.read ()

f.close ()

 
# Control jumps right here if
# any of the above lines will throw an IOError.

except IOError:

 

# print (os.error) will be & lt; class & # 39; OSError & # 39; & gt;

  print ( `Problem reading:` + filename)

 
# Either way, the code continues with
# line after try / except

Output:

 Problem reading: GFG.txt 

File Object Management

4. os.popen (): This method opens a pipe to or from a command. The return value can be read or written depending on whether the mode is & # 39; r & # 39; or & # 39; w & # 39 ;. 
Syntax :

 os.popen (command [, mode [, bufsize]]) 

Mode parameters & amp; bufsize are not required parameters, if not supplied, the mode defaults to "r".

import os

fd = " GFG .txt "

  
# popen () is like open ()

file = open (fd, `w` )

file . write ( "Hello" )

file . close ()

file = open (fd, `r` )

text = file . read ()

print (text )

 
# popen () provides a channel / gateway and accesses the file directly

file = os.popen (fd , `w` )

file . write ( "Hello" )

# File not closed, displayed in the following function.

Output:

 Hello 

Note: Output popen () will not be shown, direct changes will be made to the file.

5. os.close (): Close file descriptor fd. A file opened with open () can only be closed with close (). But a file opened with os.popen () can be closed with close () or os.close (). If we try to close a file opened with open () using os.close (), Python will throw a TypeError.

import os

fd = "GFG.txt"

file = open (fd, `r` )

text = file . read ()

print (text)

os.close ( file )

Exit:

 Traceback (most recent call last): File "C: UsersGFGDesktopGeeksForGeeksOSFile.py", line 6, in os.close (file) TypeError: an integer is required (got type _io.TextIOWrapper) 

Note: the same error cannot be issued due to lack of file permissions or permission.

6. os.rename (): the old.txt file can be renamed to new.txt using the os.rename () function. The file name only changes if the file exists and the user has sufficient permissions to modify the file.

import os

fd = " GFG .txt "

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

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

Exit:

 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` - & gt; `New.txt` 

Understanding the output: the filename" GFG.txt "exists , so the first time you use os.rename (), the file is renamed. When the os.rename () function is called again, the file "New.txt" exists and not "GFG.txt"
thus Python throws a FileNotFoundError.

Link: https://docs.python.org/2/library/os.html

This article is provided by Piyush Doorvar . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.