Opening and reading text files by buffer size

 Sometimes, we may want to read a file by buffer size so that our program does not use too much memory resources. For that, we can use the read() function which allows us to specify the buffer size that we want.


Demonstration

inputFile = open('woohoo.txt', 'r')

outputFile = open('woohooitstheoutput.txt', 'w')

msg = inputFile.read(10)

while len(msg):

    outputFile.write(msg)

    msg = inputFile.read(10)

inputFile.close()

outputFile.close()


First, we open two files, the inputFile.txt and outputFile.txt files for reading and writing respectively.


Next, we use msg = inputFile.read(10) and a while loop to loop through 10 bytes at a time. The value 10 in the parentheses tells the read() function to only read 10 bytes. while len(msg): checks the length of the variable msg. As long as the length is not zero, the loop will run.


Within the while loop, outputFile.write(msg) writes the message to the output file. After writing the message, msg = inputFile.read(10) reads the next 10 bytes and keeps doing it until the entire file is read. When that occurs, the program closes both files.


When the program is run, woohooitstheoutput.txt will be created. In the file, it will have the same contents as woohoo.txt. To prove that 10 bytes is read at a time, change outputFile.write(msg) to outputFile.write(msg + '\n') and run the program again. woohooitstheoutput.txt now has lines with at most 10 characters. Here is a segment of the output:


in order t

o guys,

yo

u need to 

fall

No comments:

Post a Comment