0

我正在使用 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。

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

4

2 回答 2

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)
于 2016-05-13T12:52:29.617 回答
0

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

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

mkdir new_folder
my_viz_prog.exe
于 2016-05-17T10:35:03.320 回答