[英]Traversing multiple symbolic links recursively in Python
I want to write a recursive function to traverse symbolic links from source path to destination path我想写一个递归 function 来遍历从源路径到目标路径的符号链接
Example: 1)readlink patha/pathb/pathc -> gives if symbolic link exists 2)readlink patha/pathb/pathc/ -> gives if symbolic link exists示例:1)readlink patha/pathb/pathc -> 如果符号链接存在则给出 2)readlink patha/pathb/pathc/ -> 如果符号链接存在则给出
I'm using os.readlink method in python to get symbolic link in Python but how to traverse multiple symbolic links我在 python 中使用 os.readlink 方法来获取 Python 中的符号链接,但是如何遍历多个符号链接
Reason for traverse: If its in future someone wants to add file3 symbolic link in between, then I wanted a recursive function to traverse each symbolic links and gives the final dest path遍历的原因:如果将来有人想在两者之间添加 file3 符号链接,那么我想要一个递归 function 来遍历每个符号链接并给出最终的 dest 路径
file1 -> file2 ->.... -> for getting destination path file1 -> file2 ->.... -> 用于获取目标路径
You could simply use你可以简单地使用
import os
def find_link(path):
try:
link = os.readlink(path)
return find_link(link)
except OSError: # if the last is not symbolic file will throw OSError
return path
print(find_link("a/b/c"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.