如何在Python中获取绝对文件路径
给定一个诸如“mydir/myfile.txt”之类的路径,如何在Python中找到该文件的绝对路径? 例如,在 Windows 上,我最终可能会得到:
"C:/example/cwd/mydir/myfile.txt"
Given a path such as "mydir/myfile.txt"
, how do I find the file's absolute path in Python? E.g. on Windows, I might end up with:
"C:/example/cwd/mydir/myfile.txt"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这总是获取当前脚本的正确文件名,即使是从另一个脚本中调用它也是如此。 当使用
subprocess
时它特别有用。从那里,您可以通过以下方式获取脚本的完整路径:
通过在目录层次结构中“向上”添加多次
/..
,您还可以更轻松地导航文件夹。获取cwd:
对于父路径:
通过将
"/.."
与其他文件名组合,您可以访问系统中的任何文件。This always gets the right filename of the current script, even when it is called from within another script. It is especially useful when using
subprocess
.from there, you can get the script's full path with:
It also makes easier to navigate folders by just appending
/..
as many times as you want to go 'up' in the directories' hierarchy.To get the cwd:
For the parent path:
By combining
"/.."
with other filenames, you can access any file in the system.今天,您还可以使用基于
path.py
的unipath
包:http://sluggo.scrapping.cc/python/unipath/我建议使用这个包,因为它提供常见 os.path 实用程序的干净接口。
Today you can also use the
unipath
package which was based onpath.py
: http://sluggo.scrapping.cc/python/unipath/I would recommend using this package as it offers a clean interface to common os.path utilities.
您可以使用它来获取特定文件的绝对路径。
You can use this to get absolute path of a specific file.
我会这样做,
返回
'/home/ecarroll/mydir/myfile.txt'
I would do it like this,
That returns
'/home/ecarroll/mydir/myfile.txt'
如果您使用的是 Mac,
这将为您提供完整路径:
将显示以下路径:
if you are on a mac
this will give you a full path:
will show the following path:
如果有人使用 python 和 linux 并寻找文件的完整路径:
In case someone is using python and linux and looking for full path to file:
安装第三方路径模块(位于
PyPI
),它将所有os.path
函数和其他相关函数包装到对象上的方法中可以在任何使用字符串的地方使用:Install a third-party path module (found on
PyPI
), it wraps all theos.path
functions and other related functions into methods on an object that can be used wherever strings are used:请注意,如果文件(或目录)名称和位置的给定表达式可能包含前导
~/
(波形符指的是用户的主目录),expandvars
负责处理任何其他环境变量(如$HOME
)。Note that
expanduser
is necessary (on Unix) in case the given expression for the file (or directory) name and location may contain a leading~/
(the tilde refers to the user's home directory), andexpandvars
takes care of any other environment variables (like$HOME
).Python 3.4+
pathlib
的更新实际上回答了问题:如果您只需要一个临时字符串,请记住,您可以将
Path
对象与os.path
,当然包括abspath
:Update for Python 3.4+
pathlib
that actually answers the question:If you only need a temporary string, keep in mind that you can use
Path
objects with all the relevant functions inos.path
, including of courseabspath
:您可以使用新的 Python 3.4 库
pathlib
。 (您还可以使用pip install pathlib
获取 Python 2.6 或 2.7 版本。)作者 写道:“这个库的目的是提供一个简单的类层次结构来处理文件系统路径以及用户对它们进行的常见操作。”要在 Windows 中获取绝对路径:
或者在 UNIX 上:
文档位于:https://docs.python.org/ 3/library/pathlib.html
You could use the new Python 3.4 library
pathlib
. (You can also get it for Python 2.6 or 2.7 usingpip install pathlib
.) The authors wrote: "The aim of this library is to provide a simple hierarchy of classes to handle filesystem paths and the common operations users do over them."To get an absolute path in Windows:
Or on UNIX:
Docs are here: https://docs.python.org/3/library/pathlib.html
如果它已经是绝对路径也有效:
Also works if it is already an absolute path: