从python找到一个文件的绝对路径 [英] finding out absolute path to a file from python

查看:417
本文介绍了从python找到一个文件的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个文件test.py驻留在某个目录中,我怎么能从test.py找到它在哪个目录? os.path.curdir将给出当前目录,但不是文件所在的目录。如果我从某个目录foo调用test.py,os.curdir会返回foo,但不会返回test.py的路径。



谢谢。 b $ b

解决方案

答案是使用:

  __file__ 

它返回一个相对路径。

  os.path.abspath(__ file__)

可用于获取完整路径。


if I have a file test.py that resides in some directory, how can I find out from test.py what directory it is in? os.path.curdir will give the current directory but not the directory where the file lives. If I invoke test.py from some directory "foo", os.curdir will return foo but not the path of test.py.

thanks.

解决方案

the answer is to use:

 __file__

which returns a relative path.

os.path.abspath(__file__) 

can be used to get the full path.

这篇关于从python找到一个文件的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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