windows上运行程序的python位置

2024-04-25 22:48:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python应用程序,需要知道它运行时在哪个目录中创建

如何知道windows上正在运行的应用程序路径,例如,当我将目录路径更改为新目录时。在

有没有一种方法可以知道python应用程序在哪里运行而我们在开始时保存它操作系统路径.abspath(操作系统路径目录名(文件

例如,以了解应用程序在何处运行os.chdir公司(“c:/”)

     import os 
     print os.path.abspath(os.path.dirname(__file__))
     os.chdir("c:/")
     print os.path.abspath(os.path.dirname(__file__))

Tags: 文件path方法路径目录应用程序oswindows
2条回答

它包含在__file__变量中。

但是如果您想知道当前的工作目录,那么应该使用^{}

>>> os.getcwd()
'C:\\Program Files\\Python31'
>>> os.chdir(r'C:\\')
>>> os.getcwd()
'C:\\'
import os
print os.path.abspath(os.path.dirname(__file__))

编辑:有点晚了!!!:) edit2:在C中,u可以使用属性AppDomain.CurrentDomain.BaseDirectory,因此使用类似的方法将有助于http://pythonnet.sourceforge.net/readme.html

相关问题 更多 >