搜索并替换2GB文本文件大小中的字符串

2024-10-01 07:40:27 发布

您现在位置: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"并替换为"Y:\here\link\path"

在python中读取和编辑大文本文件的最佳优化python方法是什么。。?你知道吗


Tags: 文件path路径txt文件夹herelinkfile
1条回答
网友
1楼 · 发布于 2024-10-01 07:40:27

遍历这些行并编写一个新文件。然后将新文件移到旧文件上。你知道吗

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)

相关问题 更多 >