首页 > 解决方案 > Python 可执行文件 - 创建目录和文件访问

问题描述

我创建了一个简单的 Python 脚本test.py,它应该检查目录是否存在,如果不存在,则创建它。

代码如下:

import os

    def crete_dir(dir_name):
        if not os.path.exists(dir_name):
            os.makedirs(dir_name)
    
    crete_dir("test_dir")

我在 Mac 上使用 PyCharm 编写和测试我的代码。

只要我在 PyCharm 中运行该脚本,它就会按预期工作,但是一旦我将它变成可执行文件(使用 pyinstaller:pyinstaller --onefile --clean --windowed test.py)并运行它,它就不起作用(不创建目录,即使它不存在)。与可执行文件一起打开的终端窗口不会返回任何文件访问权限或其他错误。

我应该怎么做才能使可执行文件正常工作?非常感谢。

标签: pythonfiledirectoryoperating-systemexecutable

解决方案


推荐阅读