gpt4 book ai didi

c - 向数千个文件添加 trec 格式标签

转载 作者:行者123 更新时间:2023-11-30 18:06:47 25 4
gpt4 key购买 nike

我需要在目录中的数千个文件中添加诸如文件文本之类的标签,我尝试使用 cat 并使用

将其输出到文件流
for file in *
do
cat ../gau > temp; //gau contain format i need to append in each file
echo $file >>temp;
cat ../gau_ >>temp ;//contains </DOCID>
cat $file >>temp;
cat ../gau1 >> temp; //this contain last sentence </DOC>
cat temp > $file
done

但是这样做非常慢,请告诉我一种更好、更有效的方法来做到这一点。操作系统不能使用c来做到这一点。我们如何批量打开文件,然后处理它们并放回,因为它可以加快这个过程因为我认为打开和写入文件是瓶颈。

是否有预制程序(高效且快速)来完成这项工作,因为我们时间紧迫。

最佳答案

这是一个快速的 python 代码,试试吧,它的执行速度比批处理脚本更快:

import os

for dirname, dirnames, filenames in os.walk('/MY_DIRECTORY/'):
for filename in filenames:
with open(os.path.join(dirname, filename), "r+") as f:
str = f.read() # read everything in the file
f.seek(0) # rewind
f.write("Prepended text tags" + str) # write the new line before
f.close()

我还没试过。

关于c - 向数千个文件添加 trec 格式标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5311134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com