繁体   English   中英

如何获取当前运行的 python 文件所在的驱动器?

[英]How can I get the drive that the currently running python file is in?

我想知道我的 python 文件所在的驱动器,因为os.abspath()没有给我,而且我没有找到任何其他可能性。

一种可能是使用:

import os
os.path.dirname(os.path.abspath(__file__))

或者要获取当前工作目录,您可以使用以下内容:

os.getcwd()

您可以尝试操作系统 package 的不同 function。

import os
print os.getcwd()

使用os.getcwd()

>>> import os
>>> os.getcwd()
path/to/folder

或者只是打印__file__

>>> __file__
path/to/folder/file.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM