将替换的路径保存在新文件中

问题描述

我正在尝试修改一个文件中的某些路径以创建新的修改文件。要修改的字符串根据原始路径而有所不同,其中需要添加一些部分以添加到新路径中。以下代码完美地打印了解决方案,但是我无法保存打印的文本。应该只将修改后的文件保存在一个文件夹中,而应返回许多包含仅包含第一行文本的文件文件夹。谢谢

@H_502_3@sourc='<SourceFilename relativetoVRT="1">'
end='</SourceFilename>'
out_vrt='C:\...\Desktop\\new_file'

file = open(inputfile,"r")
lines = file.readlines()
file.close()

for line in lines:
    if line.startswith('      <SourceFilename relativetoVRT="1">'):
        path = line.split(sourc)[1].split(end)[0]
    tile= path.split('_')[5]
    data= path.split('_')[3]
    n = path.split('_')[4]
    sat= path.split('_')[2].split('/')[2]
    if sat == 'S2A2A':
        Sat='A'
    else:
        Sat='B'
    index=path.split('_')[6][:-1]
    replaced='/.../data/'+tile +'/'+'S2_'+data +'_' + n + '_'+ tile + '_' + Sat + '_' + index +'.tif'
    out_name=vrt[0].split('\\')[5]
    VRT=out_vrt + '\\' +tile +'\\'+out_name
    old = var1+path+var2
    new=var1+replaced+var2

    os.makedirs(out_vrt + '\\' +tile,exist_ok=True) 
    filedata = line.replaced(old,new).rstrip('\n') 
    
with open(VRT,'w') as outfile:
    outfile.write(filedata)
    print(filedata)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)