April 2017
Beginner to intermediate
312 pages
7h 23m
English
Sometimes, it is necessary to read the contents of a file by line-by-file. In Python, there are two options to do this: readline() and readlines():
if __name__ == "__main__": # open text file to read file = open('read_line.txt', 'r') # read a line from the file data = file.readline() print(data) # read another line from the file data = file.readline() print(data) file.close()
When the preceding code snippet is executed (available for download as read_line_from_file.py along with this chapter), the read_line.txt file is opened and a single line is returned by the readline() function. This ...
Read now
Unlock full access