38 lines
1.1 KiB
Markdown
38 lines
1.1 KiB
Markdown
# Python 修正由于文件夹迁移导致符号连接错误的问题
|
||
|
||
FORMER_ROOT 为原文件夹路径,CURRENT_PATH 为新路径。例如,原来文件保存在 /home/usr/FormerPath 下,将其移动到 /home/usr/CurrentPath 路径下后,CurrentPath 下的符号连接文件,尤其是使用绝对路径进行连接的文件其指向路径还在 /home/usr/FormerPath 下,使用下列脚本可对其进行修复。
|
||
|
||
```py
|
||
#!/usr/bin/python
|
||
# -*- coding: utf-8 -*-
|
||
|
||
import os
|
||
|
||
FORMER_ROOT = "/home/usr/FormerPath"
|
||
CURRENT_PATH = "/home/usr/CurrentPath"
|
||
|
||
def procSLink(path):
|
||
lndir = os.readlink(path)
|
||
if lndir.startswith(FORMER_ROOT):
|
||
print "Proc SymFile: "+path
|
||
print "Sym To: "+lndir
|
||
lndir = lndir[len(FORMER_ROOT):]
|
||
npath = CURRENT_PATH+lndir
|
||
print "New Symb:"+npath
|
||
os.remove(path)
|
||
os.symlink(npath, path)
|
||
|
||
|
||
def procDir(path):
|
||
files = os.listdir(path)
|
||
for f in files:
|
||
file = path + '/' + f
|
||
if(os.path.islink(file)):
|
||
procSLink(file)
|
||
if(os.path.isdir(file)):
|
||
procDir(file)
|
||
|
||
|
||
procDir(CURRENT_PATH)
|
||
```
|