【问题标题】:Python create folder next to .exePython在.exe旁边创建文件夹
【发布时间】:2016-05-13 11:26:55
【问题描述】:

我正在使用 Vizard 从 python 脚本创建一个 .exe 文件。我需要这个脚本来创建一个位于 .exe 文件旁边的文件夹

if getattr(sys, 'frozen', False):
    logging.warning('Application is exe')
    loggingPath = os.path.dirname(sys.executable)
    logging.warning(os.getcwd())
elif __file__:
    loggingPath = os.path.dirname(__file__)
    logging.warning('Application is script')
    logging.warning(os.getcwd())

if not os.path.exists(loggingFolder):
    logging.warning('Directory not existing... creating..')
    os.makedirs(loggingFolder)

当我从 IDE 执行时工作正常,但在 exe 文件中,它会在 Windows/Users/Temp/randomfoldername 的 Appdata 文件夹中抛出数据。

另外,我总是应用程序是脚本,即使它被打包成 exe。

有人可以在这里指出正确的方向吗? 提前致谢

【问题讨论】:

  • 运行这个时你得到什么输出?
  • 从 ide 执行和作为可执行文件执行时,输出是什么?
  • 当我从 IDE 执行时:WARNING:root:Application is script USB 驱动器。来自 exe:相同的输出,但在 windows 中带有我的用户文件夹的路径。

标签: python path directory exe vizard


【解决方案1】:

sys 模块没有任何属性frozen,导致第一个if 语句总是返回False

sys.executable 将给出 python 解释器二进制文件的路径,即。对于 Windows,您的 python.exe 文件的路径,我不明白您为什么需要它。

如果你想要确保运行的文件是.exe文件,那么在它旁边创建一个文件夹,检查文件名是否以.exe结尾可能更简单?

if __file__.endswith('.exe'):
    loggingFolder = os.path.join(os.path.dirname(__file__), 'foldername')
    if not os.path.exists(loggingFolder):
        os.makedirs(loggingFolder)

【讨论】:

    【解决方案2】:

    如果您只想在运行时创建一个文件夹,那么另一种(可能更简单)的方法是从批处理文件中运行您的 vizard 程序并首先在批处理文件中创建文件夹

    例如create run_viz_prog.bat 内容如下:-

    mkdir new_folder
    my_viz_prog.exe
    

    【讨论】:

      猜你喜欢
      • 2017-01-27
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多