when the file is read in this mannerall the characters including all of the lines and newline characters are one big string in the variable inp it is good idea to store the output of read as variable because each call to read exhausts the resourcefhand open('mbox-short txt'print(len(fhand read()) print(len(fhand read()) remember that this form of the open function should only be used if the file data will fit comfortably in the main memory of your computer if the file is too large to fit in main memoryyou should write your program to read the file in chunks using for or while loop searching through file when you are searching through data in fileit is very common pattern to read through fileignoring most of the lines and only processing lines which meet particular condition we can combine the pattern for reading file with string methods to build simple search mechanisms for exampleif we wanted to read file and only print out lines which started with the prefix "from:"we could use the string method startswith to select only those lines with the desired prefixfhand open('mbox-short txt'count for line in fhandif line startswith('from:')print(linecodewhen this program runswe get the following outputfromstephen marquard@uct ac za fromlouis@media berkeley edu fromzqian@umich edu fromrjlowe@iupui edu the output looks great since the only lines we are seeing are those which start with "from:"but why are we seeing the extra blank linesthis is due to that invisible newline character each of the lines ends with newlineso the print statement