python - Resolving symlinks in Python without failing with missing components (like "readlink -m")
问题描述
GNU readlink
supports a command-line invocation similar to:
readlink -m /some/path/which/may/contain/symlinks
...which takes a path as an argument, and returns a path that does not contain any symlinks as a result (insofar as it is possible to do this), by replacing those link elements with the "real"/absolute paths they refer to. Unlike readlink -f
or readlink -e
, this tolerates (proceeds without failure) when any path element does not exist.
Is any equivalent operation available in Python?
解决方案
$ rm -rf -- /tmp/nonexisting.XYZ # just to be extra clear
$ orig_path=/tmp/nonexisting.XYZ/foo # on MacOS, where /tmp is at /private/tmp
$ greadlink -m "$orig_path" # demonstrate GNU readlink output for comparison...
/private/tmp/nonexisting.XYZ/foo
$ python -c 'import os.path, sys; print(os.path.realpath(sys.argv[1]))' "$orig_path"
/private/tmp/nonexisting.XYZ/foo
推荐阅读
- unity3d - TimeSpan 的自定义 PropertyDrawer,问题设置 SerializedProperty longValue
- java - 如何使用 ArrayList 获取 TextField 输入
- django-orm - Django ORM 按小时分组,不包括分钟
- python - 如何将具有透明背景的图像带到我的 tkinter 窗口的前面?
- gekko - 如何在 Gekko 中使用 if2/3
- mysql - 从 MySql 中的查询数据透视表中清除空值
- elasticsearch - 如何从 Elastic Search 中的 json 字段中删除字段
- r - R函数从调用它的函数继承参数
- machine-learning - 第一次运行机器学习项目
- c++ - 继承的数据如何放置(排列)在派生类中?