Python 脚本在 cx_Freeze 之后不写入文件

Python script not writing on files after 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 window 中也没有)。 我将创建一个新的专用问题,然后我将 post 在这里 link。

我知道这个帖子 2 年前就死了,但因为我遇到了同样的问题,而且我认为很难找到解决方案,所以我仍然会在这里写点东西

我的问题是 .exe 所在的文件夹。它位于一个随机文件夹中,因此它没有写入文件的权限。
我通过移动到 \Appdata\Roaming 包含构建的文件夹解决了这个问题。执行此操作后程序运行。