Linux 如何在python中找到符号链接或软链接的目标文件的完整(绝对路径)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3220755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to find the target file's full(absolute path) of the symbolic link or soft link in python
提问by duhhunjonn
when i give ls -l /etc/fonts/conf.d/70-yes-bitmaps.conf
当我给 ls -l /etc/fonts/conf.d/70-yes-bitmaps.conf
lrwxrwxrwx <snip> /etc/fonts/conf.d/70-yes-bitmaps.conf -> ../conf.avail/70-yes-bitmaps.conf
so for a symbolic link or soft link, how to find the target file's full(absolute path) in python,
所以对于符号链接或软链接,如何在python中找到目标文件的完整(绝对路径),
If i use
如果我使用
os.readlink('/etc/fonts/conf.d/70-yes-bitmaps.conf')
os.readlink('/etc/fonts/conf.d/70-yes-bitmaps.conf')
it outputs
它输出
../conf.avail/70-yes-bitmaps.conf
../conf.avail/70-yes-bitmaps.conf
but i need the absolute path not the relative path, so my desired output must be,
但我需要绝对路径而不是相对路径,所以我想要的输出必须是,
/etc/fonts/conf.avail/70-yes-bitmaps.conf
/etc/fonts/conf.avail/70-yes-bitmaps.conf
how to replace the ..
with the actual full path of the parent directory of the symbolic link or soft link file.
如何将 替换为..
符号链接或软链接文件的父目录的实际完整路径。
回答by eruciform
http://docs.python.org/library/os.path.html#os.path.abspath
http://docs.python.org/library/os.path.html#os.path.abspath
also joinpath and normpath, depending on whether you're in the current working directory, or you're working with things elsewhere. normpathmight be more direct for you.
还有 joinpath 和 normpath,这取决于您是在当前工作目录中,还是在其他地方处理。normpath对你来说可能更直接。
Update:
更新:
specifically:
具体来说:
os.path.normpath(
os.path.join(
os.path.dirname( '/etc/fonts/conf.d/70-yes-bitmaps.conf' ),
os.readlink('/etc/fonts/conf.d/70-yes-bitmaps.conf')
)
)
回答by unutbu
os.path.realpath(path)
os.path.realpathreturns the canonical path of the specified filename, eliminating any symbolic links encountered in the path.
os.path.realpath返回指定文件名的规范路径,消除路径中遇到的任何符号链接。
回答by Dave Burton
As unutbu says, os.path.realpath(path)
should be the right answer, returning the canonical path of the specified filename, resolving any symbolic links to their targets. But it's broken under Windows.
正如 unutbu 所说,os.path.realpath(path)
应该是正确的答案,返回指定文件名的规范路径,解析指向其目标的任何符号链接。但它在Windows下坏了。
I've created a patch for Python 3.2 to fix this bug, and uploaded it to:
我已经为 Python 3.2 创建了一个补丁来修复这个错误,并将其上传到:
http://bugs.python.org/issue9949
http://bugs.python.org/issue9949
It fixes the realpath()
function in Python32\Lib\ntpath.py
它修复了realpath()
功能Python32\Lib\ntpath.py
I've also put it on my server, here:
我也把它放在我的服务器上,在这里:
http://www.burtonsys.com/ntpath_fix_issue9949.zip
http://www.burtonsys.com/ntpath_fix_issue9949.zip
Unfortunately, the bug is present in Python 2.x, too, and I know of no fix for it there.
不幸的是,这个错误也存在于 Python 2.x 中,我知道那里没有修复它。
回答by Don Kirkby
The documentationsays to use os.path.join()
:
该文件说,使用os.path.join()
:
The result may be either an absolute or relative pathname; if it is relative, it may be converted to an absolute pathname using
os.path.join(os.path.dirname(path), result)
.
结果可能是绝对或相对路径名;如果是相对的,则可以使用 将其转换为绝对路径名
os.path.join(os.path.dirname(path), result)
。
回答by alpha_989
On windows 10, python 3.5, os.readlink("C:\\Users\PP")
where "C:\Users\PP" is a symbolic link (not a junction link) works.
在 Windows 10 上,python 3.5,os.readlink("C:\\Users\PP")
其中“C:\Users\PP”是一个符号链接(不是连接链接)。
It returns the absolute path to the directory.
它返回目录的绝对路径。
This works on Ubuntu 16.04, python 3.5 as well.
这也适用于 Ubuntu 16.04、python 3.5。
回答by Alex
I recommend using pathlib
library for filesystem operations.
我建议使用pathlib
库进行文件系统操作。
import pathlib
x = pathlib.Path('lol/lol/path')
x.resolve()
Documentationfor Path.resolve(strict=False)
: make the path absolute, resolving any symlinks. A new path object is returned.
文档为Path.resolve(strict=False)
:使绝对路径,解决任何符号链接。返回一个新的路径对象。