cx_Freeze 后 Python 脚本未写入文件

标签 python cx-freeze

我正在编写一个脚本,打算用 cx_Freeze 卡住。我使用的是 Python 3.6 和 cx_Freeze 5.1.1。

我目前面临的问题是我的Python脚本——完美地工作为.py——一旦用cx_Freeze卡住,就会读取text.txt的内容 文件,但似乎无法在其上写入。

我已经编写了我正在尝试做的事情的简化版本,但问题仍然存在。

这是我的main.py:

from tkinter import *

def writing():
    word = str(word_field.get())
    ft = open('text.txt', 'w')
    ft.write(word)
    ft.close()

def from_file():
    ft = open('text.txt', 'r')
    string = ''
    for line in ft:
        line = line.strip()
        string = string+line
    ft.close()

    root2 = Tk()
    result = Label(root2, text=string)
    result.grid(row=1, column=1)
    root2.mainloop()

 root = Tk()
 root.title('My window')
 word_field = Entry(root)
 btn_1 = Button(root, text='Read', command=from_file)
 btn_2 = Button(root, text='Write', command=writing)

 word_field.grid(row=1, column=1, columnspan=2)
 btn_1.grid(row=2, column=1)
 btn_2.grid(row=2, column=2)

 root.mainloop()

这是我用于 cx_Freeze 的 setup.py

from cx_Freeze import setup, Executable
import os.path

PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

setup(
    name = "Prova",
    version = "1.0.0",
    options = {"build_exe": {
            'packages': ["tkinter"],
            'include_files' : [os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), \
            os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), 'text.txt'],
            'include_msvcr': True,
            }},
        executables = [Executable("main.py", base="Win32GUI")]
        )

知道为什么它会这样吗? 预先感谢您!!

最佳答案

关于这个问题的更新:经过大量不同的配置(我什至尝试使用 PyInstaller 而不是 cx_Freeze),结果发现问题不在于脚本或卡住过程本身,而在于事实上,由于可执行文件需要在文件上写入,这与赋予可执行文件的权限相冲突。

这意味着可执行文件无法写入该文件,程序停止但不会生成错误消息(甚至不在 cmd 窗口中运行它)。 我将创建一个新的专用问题,然后将其链接发布到此处。

关于cx_Freeze 后 Python 脚本未写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48026079/

相关文章:

python-3.x - 使用 cx_Freeze 将 OpenGL 脚本从 .py 转换为 .exe 时出现 NoneType 错误

python - lxml xpath 的语法问题

python - 安装后 cx_freeze 无法获取 zip 导入器

Python:使用函数导入许多变量

python - 一个应该用 Python 写出所有正确括号的程序

python - 如何使用 cx_Freeze bundle 含 SQLObject 的应用程序

python - 在 anaconda 上安装 cx_freeze?

python - 卸载通过 cx_freeze bdist_msi 创建的先前安装的 msi

python - Python 3 中的 Google OAuth 错误

python - 具有基于类的 View 的装饰器