【问题标题】:python location of the running program on windowswindows上正在运行的程序的python位置
【发布时间】:2010-10-20 13:00:44
【问题描述】:

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

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

有没有办法知道 python 应用程序在哪里运行而我们在开头保存它 os.path.abspath(os.path.dirname(file))

例如知道应用程序在 os.chdir("c:/") 之后运行的位置

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

【问题讨论】:

标签: python command-line path


【解决方案1】:

它包含在__file__ 变量中。

但是如果你想知道当前的工作目录,那么你应该使用os.getcw

>>> os.getcwd()
'C:\\Program Files\\Python31'
>>> os.chdir(r'C:\\')
>>> os.getcwd()
'C:\\'

【讨论】:

    【解决方案2】:
    import os
    print os.path.abspath(os.path.dirname(__file__))
    

    编辑:有点晚了!!! :) edit2:在 C# 中你可以使用属性 AppDomain.CurrentDomain.BaseDirectory 所以使用类似这样的东西将有助于http://pythonnet.sourceforge.net/readme.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多