如何在Python中获取当前文件目录的完整路径? [英] How to get full path of current file's directory in Python?
本文介绍了如何在Python中获取当前文件目录的完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
>>> ;> os.path.abspath(__ file__)
'C:\\python27\\test.py'
但是我怎样才能检索目录的路径?
例如:
'C:\\python27\\'
code $ <$ $ p
解决方案如果您的意思是正在运行的脚本的目录:
import os
os.path.dirname(os.path.abspath(__ file__))
$ b 如果您是指当前工作目录:
import
os.getcwd()
请注意,在 file
是两个下划线,而不仅仅是一个。
I want to get the current file's directory path.
I tried:
>>> os.path.abspath(__file__)
'C:\\python27\\test.py'
But how can I retrieve the directory's path?
For example:
'C:\\python27\\'
解决方案 If you mean the directory of the script being run:
import os
os.path.dirname(os.path.abspath(__file__))
If you mean the current working directory:
import os
os.getcwd()
Note that before and after file
is two underscores, not just one.
这篇关于如何在Python中获取当前文件目录的完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文