在Python中,我可以用什么命令来查找。
要获得一个Python文件所包含的目录的完整路径,在该文件中这样写。
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
(注意,如果你已经使用了os.chdir()
来改变你的当前工作目录,上面的咒语就不会起作用,因为__file__
常量的值是相对于当前工作目录的,不会因为os.chdir()
的调用而改变。)
要获得当前的工作目录,可以使用
import os
cwd = os.getcwd()
以上使用的模块、常量和函数的文档参考。
os
和 os.path
模块。__file__
常量os.path.realpath(path)
(返回 "指定文件名的规范路径,消除路径中遇到的任何符号链接")os.path.dirname(path)
(返回 "路径path
的目录名称")os.getcwd()
(返回"代表当前工作目录的字符串")os.chdir(path)
("将当前工作目录改为path
")当前工作目录。 os.getcwd()
。
而__file__
属性可以帮助你找出你正在执行的文件的位置。 这篇SO文章解释了一切:https://stackoverflow.com/questions/2632199/how-do-i-get-the-path-of-the-current-executed-file-in-python
你可能会发现这个作为参考很有用。
import os
print("Path at terminal when executing this file")
print(os.getcwd() + "\n")
print("This file path, relative to os.getcwd()")
print(__file__ + "\n")
print("This file full path (following symlinks)")
full_path = os.path.realpath(__file__)
print(full_path + "\n")
print("This file directory and name")
path, filename = os.path.split(full_path)
print(path + ' --> ' + filename + "\n")
print("This file directory only")
print(os.path.dirname(full_path))
pathlib
模块,在 Python 3.4 中 引入 (PEP 428 - pathlib 模块 - 面向对象的文件系统路径),使得路径相关的体验大为改善。
$ pwd
/home/skovorodkin/stack
$ tree
.
└── scripts
├── 1.py
└── 2.py
为了获得当前的工作目录,使用Path.cwd()
。
from pathlib import Path
print(Path.cwd()) # /home/skovorodkin/stack
要获得脚本文件的绝对路径,请使用Path.resolve()
方法。
print(Path(__file__).resolve()) # /home/skovorodkin/stack/scripts/1.py
如果要得到你的脚本所在目录的路径,请访问.parent
(建议在.parent
之前调用.resolve()
)。
print(Path(__file__).resolve().parent) # /home/skovorodkin/stack/scripts
记住__file__
在某些情况下是不可靠的。
如何在Python中获取当前执行文件的路径?。
请注意,Path.cwd()
、Path.resolve()
和其他Path
方法返回的是路径对象 (PosixPath
),而不是字符串。
在Python 3.4和3.5中,这造成了一些痛苦,因为open
内置函数只能处理字符串或字节对象,而不支持Path
对象,所以你必须将Path
对象转换为字符串,或者使用Path.open()
方法,但后一种选择需要你修改旧代码。
$ cat scripts/2.py
from pathlib import Path
p = Path(__file__).resolve()
with p.open() as f: pass
with open(str(p)) as f: pass
with open(p) as f: pass
print('OK')
$ python3.5 scripts/2.py
Traceback (most recent call last):
File "scripts/2.py", line 11, in <module>
with open(p) as f:
TypeError: invalid file: PosixPath('/home/skovorodkin/stack/scripts/2.py')
正如你所看到的那样,open(p)
在Python 3.5中不能工作。
PEP 519 - 添加文件系统路径协议](https://www.python.org/dev/peps/pep-0519/),在 Python 3.6 中实现,为 open
函数增加了对 PathLike
对象的支持,所以现在您可以直接将Path
对象传递给open
函数。
$ python3.6 scripts/2.py
OK
如果你使用的是Python 3.4,那么有一个全新的更高级别的pathlib
模块,它允许你方便地调用pathlib.Path.cwd()
来获取一个代表你当前工作目录的Path
对象,以及许多其他新特性。
关于这个新API的更多信息可以在这里找到。
1:
**回答#1:***
如果你想要当前目录,请这样做。
import os
os.getcwd()
如果你只想知道任何文件夹的名称,并且你有该文件夹的路径,请这样做。
def get_folder_name(folder):
'''
Returns the folder name, given a full folder path
'''
return folder.split(os.sep)[-1]
回答#2:。
import os
print os.path.abspath(__file__)
对于问题1,使用os.getcwd() #获取工作区
和os.chdir(r'D:Steam\steamapps\common') #设置工作区
。
我建议问题2使用sys.argv[0]
,因为sys.argv
是不可变的,因此总是返回当前文件(模块对象路径),不受os.chdir()
影响。
你也可以这样做。
import os
this_py_file = os.path.realpath(__file__)
# vvv Below comes your code vvv #
但是这段代码和sys.argv[0]
在PyInstaller编译时将无法工作或工作得很奇怪,因为在__main__
级别没有设置魔法属性,而且sys.argv[0]
是你的exe被调用的方式(意味着它将受到工作目录的影响)。