使用python re sub将映射的驱动器k:/和k:\替换为驱动器中所有文件的unc路径

2024-06-16 09:59:57 发布

您现在位置:Python中文网/ 问答频道 /正文

在对驱动器中的所有文件进行迭代之后,我希望使用unc路径更改对映射驱动器的所有引用

我不明白为什么这不管用,或者这是不是正确的方法。你知道吗

    with open(file, 'w') as ts:
        for item in c:
           itemchange=re.compile(re.escape('k:/'),re.IGNORECASE)
           itemswitch=itemchange.sub('//covenas/decisionsupport/',item)
           itemchange=re.compile(re.escape('k:\'),re.IGNORECASE)
           itemswitch=itemchange.sub('//covenas/decisionsupport/',itemswitch)
           itemchange=re.compile(re.escape('i:/'),re.IGNORECASE)
           itemswitch=itemchange.sub('//covenas/spssdata/',itemswitch)
           itemchange=re.compile(re.escape('i:\'),re.IGNORECASE)
           itemswitch=itemchange.sub('//covenas/spssdata/',itemswitch)
           ts.write(itemswitch) 

本节在后面

from os import walk

def walkChange(mypath):
    f = []
    for (dirpath, dirnames, filenames) in walk(mypath):
        for file in filenames:
            f.append(dirpath+file)
    return f

mypath='k:/meinzer/production/'
f=walkChange(mypath)

list=[]
[list.append(i) for i in f if i.upper().endswith('SPS')]
#[i for i in list]
#testing with a file
#file='K:/meinzer/production/ps/development code/testd.sps'
for file in list:
    with open(file, "r") as f: 
        c=f.readlines()  
    with open(file, 'w') as ts:
        for item in c:
           itemchange=re.compile(re.escape('k:/'),re.IGNORECASE)
           itemswitch=itemchange.sub('//covenas/decisionsupport/',item)
           itemchange=re.compile(re.escape('k:\'),re.IGNORECASE)
           itemswitch=itemchange.sub('//covenas/decisionsupport/',itemswitch)
           itemchange=re.compile(re.escape('i:/'),re.IGNORECASE)
           itemswitch=itemchange.sub('//covenas/spssdata/',itemswitch)
           itemchange=re.compile(re.escape('i:\'),re.IGNORECASE)
           itemswitch=itemchange.sub('//covenas/spssdata/',itemswitch)
           ts.write(itemswitch) 

Tags: inreforwithitemfilecompilets