April 2017
Beginner to intermediate
312 pages
7h 23m
English
We discussed reading and writing to files using the r and w flags. There is another called r+. This flag enables reading and writing to a file. Let's review an example that enables us to understand this flag.
Let's review the contents of write_file.txt once again:
I am excited to learn Python using Raspberry Pi Zero This is a line appended to the file
Let's modify the second line to read: This is a line that was modified. The code sample is available for download along with this chapter as seek_to_write.py.
if __name__ == "__main__": # open text file to read and write file = open('write_file.txt', 'r+') # set the pointer to the desired position file.seek(68) file.write('that was modified \n') # rewind the pointer to the beginning ...Read now
Unlock full access