NotePublic/Software/Development/Language/Python/Python_修正由于文件夹迁移导致符号连接错误的问题.md

38 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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)
```