2017-05-11 28 views
0
[[email protected]]# ls -lad /etc/ecm/zookeeper-conf 
lrwxrwxrwx 1 root root 29 May 11 19:51 /etc/ecm/zookeeper-conf -> /etc/ecm/zookeeper-conf/3.4.6 

具有/etc/ecm/zookeeper-conf/etc/ecm/zookeeper-conf/3.4.6/etc/ecm/zookeeper-conf/3.4.6一個smylink不存在。我如何覆蓋不好的符號鏈接與蟒蛇

所以,當我運行ln -s /etc/ecm/zookeeper-conf-3.4.6 /etc/ecm/zookeeper-conf,我得到ln: failed to access '/etc/ecm/zookeeper-conf': Too many levels of symbolic links

1.How與ln強行建立一個smylink?

2.如何判斷python是否存在錯誤的符號鏈接?如果您運行os.path.exists(bad-link),你會得到False

回答

1

第一個問題。

因爲你的邏輯不正確。根本原因是因爲您需要使用版本找到正確的原始文件,而不是重新構建軟鏈接。

因此...

/etc/ecm/zookeeper-conf-3.4.6 <== is not exist 

ln -s /etc/ecm/zookeeper-conf-3.4.6 /etc/ecm/zookeeper-conf <== this is not incorrect 

#find the exist version zookeeper-conf-x.x.x 
ln -s /etc/ecm/zookeeper-conf-x.x.x /etc/ecm/zookeeper-conf <== this is correct 

關於第二個問題,這可能會使用shell命令(LS)來解決。 ls -L $symbolic_link檢查該鏈接是否有效。如果鏈接的原始文件不存在,它將打印No such file or directory並返回非零值。

例如:

$ ls -al 
symlink1 -> ./exist.txt 
symlink2 -> ./not_exist.txt 
exist.txt 

$ ls -L symlink1 
symlink1 

$ ls -L symlink2 
ls: symlink2: No such file or directory 

Python的命令行溶液

def check_symlink_exist(target_file): 
    process = subprocess.Popen("ls -L " + target_file, shell=True, stdout=subprocess.PIPE) 
    process.wait() 
    return True if process.returncode == 0 else False 

的Python OS溶液

def check_symlink_exist(target_file): 
    try: 
     os.stat(target_file) 
     return True 
    except OSError: 
     return False