迭代驱动器中的所有文件后,我想用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)
答案 0 :(得分:1)
你的台词:
itemchange=re.compile(re.escape('k:\'),re.IGNORECASE)
(和re.escape('i:\')
)需要将反斜杠加倍。我很震惊,并没有给你一个令人讨厌的SyntaxError