Python 搜索和替换2GB文本文件大小的字符串
我想搜索和替换文本文件中的一些文件路径。 我的文件大小平均在2GB以上 例如: 我的D驱动器中有一个文件夹“D:\LargeFilesFolder”。 并且所有文件都存在于该文件夹中Python 搜索和替换2GB文本文件大小的字符串,python,Python,我想搜索和替换文本文件中的一些文件路径。 我的文件大小平均在2GB以上 例如: 我的D驱动器中有一个文件夹“D:\LargeFilesFolder”。 并且所有文件都存在于该文件夹中 "D:\LargeFilesFolder\large_file_v001.txt" "D:\LargeFilesFolder\large_file_v002.txt" "D:\LargeFilesFolder\large_file_v003.txt" 在所有文本文件中,我要搜索“X:\path\link\here
"D:\LargeFilesFolder\large_file_v001.txt"
"D:\LargeFilesFolder\large_file_v002.txt"
"D:\LargeFilesFolder\large_file_v003.txt"
在所有文本文件中,我要搜索“X:\path\link\here”
,并替换为“Y:\here\link\path”
在python中读取和编辑大文本文件的最佳优化python方法是什么?迭代这些行并编写一个新文件。然后将新文件移到旧文件上
with open(outputfilename, "w") as outputfile:
with open(inputfilename, "r") as inputfile:
for line in inputfile:
# replace in line and write to outputfile
shutil.move(outputfilename, inputfilename)
迭代这些行并编写一个新文件。然后将新文件移到旧文件上
with open(outputfilename, "w") as outputfile:
with open(inputfilename, "r") as inputfile:
for line in inputfile:
# replace in line and write to outputfile
shutil.move(outputfilename, inputfilename)
迭代这些行并编写一个新文件。然后将新文件移到旧文件上。对于使用open(大文本文件,'r')作为txt文件在行上进行迭代,这将加载整个文件,但由于文件大小较大。我想用内存优化的方法来做。不,它不会将整个文件读入内存。试试看。在这些行上迭代,然后写一个新文件。然后将新文件移到旧文件上。对于使用open(大文本文件,'r')作为txt文件在行上进行迭代,这将加载整个文件,但由于文件大小较大。我想用内存优化的方法来做。不,它不会将整个文件读入内存。试试看。