2022年1月26日 星期三

[ Python 常見問題 ] How to get symbolic link target in Python?

 Source From Here

Question
How do I extract the target path from a symbolic link?

HowTo
The problem with os.readlink() is it will only resolve 1 step of the link. We can have a situation where A links to another link B, and B link is dangling:
$ ln -s /tmp/example/notexist /tmp/example/B
$ ln -s /tmp/example/B /tmp/example/A
$ ls -l /tmp/example
A -> /tmp/example/B
B -> /tmp/example/notexist

Now in Python, os.readlink() gives you the first target:
>>> import os
>>> os.readlink('A')
'/tmp/example/B'

But in most situations I assume we are interested in the resolved path. So pathlib can help here:
>>> from pathlib import Path
>>> Path('A').resolve()
PosixPath('/tmp/example/notexist')

This message was edited 5 times. Last update was at 25/01/2022 20:27:24

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...