如何在python中找到目标文件的符号链接或软链接的完整(绝对路径) [英] how to find the target file's full(absolute path) of the symbolic link or soft link in python

查看:1513
本文介绍了如何在python中找到目标文件的符号链接或软链接的完整(绝对路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我给 ls -l/etc/fonts/conf.d/70-yes-bitmaps.conf

when i give 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

因此对于符号链接或软链接,如何在python中查找目标文件的完整(绝对路径),

so for a symbolic link or soft link, how to find the target file's full(absolute path) in python,

如果我使用

os.readlink('/etc/fonts/conf.d/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

如何用符号链接或软链接文件的父目录的实际完整路径替换...

how to replace the .. with the actual full path of the parent directory of the symbolic link or soft link file.

推荐答案

os.path.realpath(path)

os.path.realpath 返回规范指定文件名的路径,消除了路径中遇到的任何符号链接.

os.path.realpath returns the canonical path of the specified filename, eliminating any symbolic links encountered in the path.

这篇关于如何在python中找到目标文件的符号链接或软链接的完整(绝对路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆