在 fileinput.Fileinput (或其他模块/类)中是否有一种方法可以将整个文件读入内存,以便我可以用列表中的文本 block 替换文件中的文本 block (保存在内存)。
有一个 .readline 函数,但我需要查找并替换文本 block ,并且我不希望在压缩文件的同时逐行压缩文本 block 。
最佳答案
是的,file
对象有一个read
方法,可以将整个文件内容读入内存。
with open("myfile.txt") as infile:
text = infile.read()
# manipulate text here
with open("myfile.txt", "w") as outfile:
outfile.write(text)
关于python - fileinput.Fileinput .read() 整个文件的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36750576/