Read all the characters into a string (slurp)
In some other cases, especially if you are looknig for some pattern that starts on one line but ends on another line. you'd be better off having the whole file as a single string in a variable. This is where the read method comes in handy.
It can also be used to read in chunks of the file.
filename = 'examples/files/numbers.txt' with open(filename, 'r') as fh: lines_str = fh.read() # reads all the lines into a string print(len(lines_str)) # number of characters in file print(lines_str) # the content of the file
read(20) will read 20 bytes.