我想搜索并替换文本文件中的一些文件路径.平均而言,我的文件大小超过2 GB.
例如:我的D驱动器中有一个文件夹"D:\ LargeFilesFolder".并且文件夹中存在所有文件.
"D:\LargeFilesFolder\large_file_v001.txt"
"D:\LargeFilesFolder\large_file_v002.txt"
"D:\LargeFilesFolder\large_file_v003.txt"
Run Code Online (Sandbox Code Playgroud)
在我要搜索"X:\path\link\here" 和替换的所有文本文件中"Y:\here\link\path"
在python中读取和编辑大文本文件的最佳优化python方法是什么?
小智 5
迭代行并写一个新文件.然后将新文件移到旧文件上.
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
82 次 |
| 最近记录: |