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”
posix pre >
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.
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.
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 ;.
os.popen (command [, mode [, bufsize]])
Mode parameters & amp; bufsize are not required parameters, if not supplied, the mode defaults to "r".
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.
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.
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.
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.