May 2017
Intermediate to advanced
280 pages
6h 2m
English
Now we will read a file by a program. The access mode for reading is r.
Let's take the sample of a text file containing famous quotes:

I have saved the preceding file with the name sample1.txt.
Let's write a readfile.py program to read the earlier file:
file_input = open("sample1.txt",'r')all_read = file_input.read()print all_readfile_input.close()
In the given code, first we created a file_input file object, then we called file_input.read() to read the file content. After reading, the file_input file object is closed by file_input.close(). To be sure the sample file and the code readfile.py must be in the ...
Read now
Unlock full access