Is there a way in fileinput.Fileinput (or some other module/class) where I can read the entire file into memory so I can replace a block of text from the file with a block of text in a list (saved in memory).
There is a .readline function, but I need to find & replace a block of text, and I would prefer not having to zip through the block of text line by line in parallel with zipping through the file.
Yes, file
objects have a read
method that reads the entire file contents into memory.
with open("myfile.txt") as infile:
text = infile.read()
# manipulate text here
with open("myfile.txt", "w") as outfile:
outfile.write(text)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.