You might have used inbuilt functions to perform operations on files in python. In this article, we will try to implement file handling using os module to perform the file operations like python read file, write to file and append to file using different functions in python.
How to use os module to implement file handling in Python?
The os module can be used to perform different operations on file systems using python. Unlike built-in functions, the os module reads and writes data to the file system in the form of bytes. The methods like open() , write(), read() and close() are also defined in the os module with different specifications and properties compared to in built functions. To use the methods, we can import the os module as follows.
Open a file using os module
To open a file in python using the os module, we can use the open() method. The open() method takes two arguments as input. The first argument is the filename which needs to be opened and the second argument is the mode in which the file has to be opened. The most common mode parameters are defined as follows.
- os.O_RDONLY mode is used for opening a file in read only mode.
- os.O_WRONLY mode is used for opening a file in write only mode.
- os.O_RDWR mode is used for opening a file for both reading and writing to it.
- os.O_APPEND mode is used for opening a file in append mode and the data is appended to the file on each write operation when the file is opened in this mode.
- os.O_CREAT mode is used to create a file and then open it when the file does not exist.
The open() method returns a file descriptor on successfully opening the file. A file can be opened using specifying the filename and mode as follows.
We can use one or more modes simultaneously while opening the file with the help of pipe operator “|” as follows.
Close a file
In python, we must close all the opened files before termination of the program using the close() method to perform successful write operations. The close() method in the os module can be used for the same.The close() method takes the file descriptor as input argument and closes the file on successful completion.
While doing file operations, we must use exception handling using python try except and invoke the close() method in finally block so that file gets closed even if any error occurs during execution of file. An opened file can be closed using close() method as follows.
Read a file using os module
We can use the read() method to read a file using the os module in python. The read() method takes the file descriptor as its first argument and number of bytes to be read as second argument and returns a byte string which needs to be decoded using decode() method to get the actual data.
The decode() method when invoked on a byte string takes the encoding type of the file to be decoded and returns the data in string format.
We can use the read() and the decode() method to read data from a file as follows.
import os try: myFile=os.open("/home/aditya1117/filename.txt",os.O_RDONLY) myData=os.read(myFile,105) myStr=myData.decode("UTF-8") print(myStr) except Exception as e: print(str(e)) finally: os.close(myFile)
This is a sample text file. This file is for PythonForBeginners.com. This file has been written by Aditya
Write to file using os module
We can use the write() method to write data into the filesystem using the os module. The write() method takes the file descriptor as its first argument and the data to be written to file in byte format as second argument. It writes the data to the file from start of the file descriptor. On completing a successful write operation, it returns the number of bytes written to the file.
To convert our data to byte format, we will use encode() method. The encode() method when invoked on any object takes the encoding format as input and returns the data in byte format.
First we will open the file in os.O_WRONLY or os.O_RDWR mode and then we can use the encode() method and write() method to write any data into a file system using the os module in python as follows.
import os try: myFile=os.open("/home/aditya1117/filename1.txt",os.O_WRONLY) myStr="Hi This is Pythonforbeginners.com" myData=myStr.encode("UTF-8") os.write(myFile,myData) except Exception as e: print(str(e)) finally: os.close(myFile)
To perform an append operation, we just need to open the file using os.O_APPEND along with os.Owronly mode and then we can append data into the file using the write() method as follows.
import os try: myFile = os.open("/home/aditya1117/filename2.txt", os.O_WRONLY | os.O_APPEND) myStr = "Hi! This is Pythonforbeginners.com" myData = myStr.encode("UTF-8") os.write(myFile, myData) except Exception as e: print(str(e)) finally: os.close(myFile)
In this article, we have used the os module to implement the different operations on files in python. Stay tuned for more informative articles.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.