Python Programming - write() and read() Methods

Python Programming – write() and read() Methods

Alike, C, C++, and Java, data can be written and read from the file by using some methods in Python. For writing data into the file write() method is used. However, for reading data from the file read() method is used. These methods are very easy to use in Python and described in the following sub-sections.

Writing to a File

In order to perform a write operation on a file, it must be opened first in either write mode ‘w’, append mode ‘a’, or exclusive mode ‘x\ While opening a file in ‘w’ mode, some caution is required. If file to be opened already exists then it will overwrite the contents of the file. That means, all the previous contents of the file will be lost or erased. Writing data to the file is accomplished by using write() method. This method returns the number of characters written to the file. The syntax of writeQ method is given as follows:

fileObj ect. write(string)

Code: 9.7. Illustration of writeQ method.

# Illustration of write() method

with open(“data.txt”,’w’,encoding = ‘cpl252’) as fp:
fp.write(“This is an illustration \n”)
fP.write(“of writing/n”)
fp.write(“data to the file\n”)
fp.close( );

The program presented in Code 9.7 shows that how data is written to the file. This program creates a new file named ‘data.txt’ if it does not exist. If it already exists, it is overwritten. In order to differentiate different lines newline character (\n) can be used as shown in the program. Since we are working in Windows, ‘cpl252’ encoding is used while opening the file. After performing a writing operation on the file using the write() method, file is closed through the file object and close() function. We see that the ‘with’ clause used with the openQ function for opening the file, this is done to avoid the occurrence of any exception during the execution of the program. This is an alternate of using try … finally clause, as described in the previous section.

Reading from a File

In the previous section, we have learnt how to write data into the file. Now, we will learn how to read the contents of a file. This can be accomplished by using the read() method. But, before that the file must be opened in read mode ‘r\ The syntax of read() method is given as follows:

fileObject.read(size)

In the above syntax, we see that size argument is specified in the read() method. It determines the number of characters to be read by
Programming in Python the method. If the size is not mentioned, then it reads and returns all the characters upto end of file. The illustration of using read() method is presented in Code 9.8. In this program, we read the data, which was written to a data.txt file in the previous program given in Code 9.7. Initially, the file is opened in the read mode ‘r’ and with windows encoding scheme. Then data is read character by character by specifying its size. This method inserts newline character by itself after reading specified number of characters. In order to read the entire line or whole text at one go, the read() method can be used without specifying the number of characters in the argument as done in the second last line of code in the program.

Code:9.8. Illustration of read ( ) method.

# illustration of read() method

with open(“data.txt”,’r’,encoding = ‘cpl252’) as fp:
print(fp.read(5))
print(fp.read(5))
print(fp.read())
fp.close( );

Output

This
is an
illustration
of writing
data to the file

Another method of reading the contents of a file is by using readline(). The example of this method is given in code 9.9. We see from the program that the readline() method reads and returns the entire line as written into the file. When EOF (end of file) is reached, it returns the empty string. If programmer wishes to read and print the entire file at one time, then readlines( ) method can be used. It reads all the lines’ and returns to the user. The program for the same is given in Code,9.10.

Code: 9.9. Illustration of readline( ) method.

 

# illustration of readline() method

with open(“data.txt”,’r’,encoding = ‘cp 1252’) as fp:
print(fp.readline())
print(fp.readline( ))
print(fp.readline())
print(fp.readline( ))
fp.close( );

Output

This is an illustration

of writing

data to the file

Code: 9.10. Illustration of readlines( ) method.

# illustration of readline() method

with open(“data.txt”,’r’,encoding = ‘cpl252’) as fp:
print(fp .readlines( ))
fp.close( );

Output

[‘This is an illustration \n’, ‘of writing\n’, ‘data to the file\n’]

Another way is to read the contents of file by using the for loop. This method is easier and efficient than the readQ and readlineQ methods. The illustration for reading a file using for loop is given in Code: 9.11.

Code: 9.11. Illustration of reading a file using for loop.

# illustration of reading a file using for loop

with open(“data.txt”,’r’,encoding = ‘cpl252’) as fp:
for line in fp:
print(line)
fp.close( );

Output

This is an illustration

of writing

data to the file

Python Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *