The OS module in python provides functions for interacting with the operating system. OS, comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. The *os* and *os.path* modules include many functions to interact with the file system.
Following are some functions in OS module:
1. os.name: This function gives the name of the operating system dependent module imported. The following names have currently been registered: ‘posix’, ‘nt’, ‘os2’, ‘ce’, ‘java’ and ‘riscos’
Note: It may give different output on different interpreters, such as ‘posix’ when you run the code here.
2. os.getcwd(): Function os.getcwd(), returns the Current Working Directory(CWD) of the file used to execute the code, can vary from system to system.
Note: In case of GFG interpreter, directory used is \root.
3. os.error: All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. os.error is an alias for built-in OSError exception.
Problem reading: GFG.txt
File Object Manipulation
4. 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’.
os.popen(command[, mode[, bufsize]])
Parameters mode & bufsize are not necessary parameters, if not provided, default ‘r’ is taken for mode.
Note: Output for popen() will not be shown, there would be direct changes into the file.
5. os.close(): Close file descriptor fd. A file opened using open(), can be closed by close()only. But file opened through os.popen(), can be closed with close() or os.close(). If we try closing a file opened with open(), using os.close(), Python would throw TypeError.
Traceback (most recent call last): File "C:\Users\GFG\Desktop\GeeksForGeeksOSFile.py", line 6, in os.close(file) TypeError: an integer is required (got type _io.TextIOWrapper)
Note: The same error may not be thrown, due to non-existent of file or permission privilege.
6. os.rename(): A file old.txt can be renamed to new.txt, using the function os.rename(). The name of the file changes only if, the file exists and user has sufficient privilege permission to change the file.
Traceback (most recent call last): File "C:\Users\GFG\Desktop\ModuleOS\GeeksForGeeksOSFile.py", line 3, in os.rename(fd,'New.txt') FileNotFoundError: [WinError 2] The system cannot find the file specified: 'GFG.txt' -> 'New.txt'
Understanding the Output: A file name “GFG.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 “GFG.txt”
thus Python throws FileNotFoundError.
This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- colorsys module in Python with examples
- Python datetime module with examples
- MySQL-Connector-Python module in Python
- twitter-text-python (ttp) module - Python
- Import module in Python
- struct module in Python
- Secrets | Python module to Generate secure random numbers
- Python calendar module | formatmonth() method
- Python | Writing to an excel file using openpyxl module
- Count frequencies of all elements in array in Python using collections module
- Stack and Queue in Python using queue Module
- Testing in Python using doctest module
- Python | ASCII art using pyfiglet module
- Python | Adjusting rows and columns of an excel file using openpyxl module
- Python | Plotting charts in excel sheet using openpyxl module | Set - 1
- Python | Convert Image to Pdf using img2pdf module
- Python | Plotting charts in excel sheet using openpyxl module | Set – 2
- Python | Plotting charts in excel sheet using openpyxl module | Set 3
- Python | Create and write on excel file using xlsxwriter module
- spwd module in Python
Improved By : Akanksha_Rai