gpt4 book ai didi

python - 使用python删除和修改一堆文件中的文本

转载 作者:行者123 更新时间:2023-12-01 04:51:41 25 4
gpt4 key购买 nike

我有一堆名为 test211.cshtest212.csh 等的文件。我拥有的每个文件的第一行:

"#$ -N /gridware/wor(number without 2)"

"#$ -o /gridware/wor(number without 2).out"

"#$ -e /gridware/wor(number without 2).err"

"#$ -cwd"

(没有 "")

例如test238.csh:

"#$ -N /gridware/wor38"

"#$ -o /gridware/wor38.out"

"#$ -e /gridware/wor38.err"

"#$ -cwd"

我只想在同一个文件中:

"#$ -N (name of the file without CSH)"

我还想将 "//" 替换为 "/"

我怎样才能做到这一点?

最佳答案

import os
from glob import glob
from itertools import islice
path = "full_path/"
import ntpath

for f in glob(path + "*.csv"): # find all .csh files
with open(os.path.join(path, f)) as fl:
data = fl.readlines()
with open(os.path.join(path, f), "w") as w:
# write filename - .csh
w.write("#$ -N {}\n".format(ntpath.basename(f).rstrip(".csh")))
for line in data[4:]:
# write remaining lines
w.write(line.replace("//", "/"))

关于python - 使用python删除和修改一堆文件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28304167/

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