[英]Python script not writing on files after cx_Freeze
I am working on a script I intend to freeze with cx_Freeze.我正在编写一个我打算用 cx_Freeze 冻结的脚本。 I'm using Python 3.6 and cx_Freeze 5.1.1.
我使用的是 Python 3.6 和 cx_Freeze 5.1.1。
The problem I am facing at the moment is that my Python script -- perfectly working as .py
--, once frozen with cx_Freeze, does read the content of a text.txt
file but seems unable to write on it.我目前面临的问题是我的 Python 脚本——完美地作为
.py
工作——一旦被 cx_Freeze 冻结,确实读取了text.txt
文件的内容,但似乎无法在上面写入。
I have written a simplified version of what I'm trying to do and the problem is still there.我已经写了一个我正在尝试做的简化版本,但问题仍然存在。
This is my main.py
:这是我的
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()
And this is the setup.py
that I used for cx_Freeze这是我用于 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")]
)
Any idea about why it does behave like this?知道它为什么会这样吗? Thank you in advance!!
先感谢您!!
An update on this question: After a good number of different configurations (and I even tried to use PyInstaller instead of cx_Freeze), it comes out that the problem wasn't in the script or in the freezing process itself but in the fact that, as the executable file requires to write on a file this is in conflict with the privileges given to the executable.关于这个问题的更新:经过大量不同的配置(我什至尝试使用 PyInstaller 而不是 cx_Freeze),结果发现问题不在于脚本或冻结过程本身,而是事实上,由于可执行文件需要写入文件,这与赋予可执行文件的权限冲突。
This means that the executable cannot write on the file, the program stops but no error message is generated (not even running it in the cmd window).这意味着可执行文件无法写入文件,程序停止但不会生成错误消息(甚至不在 cmd 窗口中运行它)。 I will create a new dedicated question and I will then post here the link to it.
我将创建一个新的专门问题,然后我将在此处发布它的链接。
I know this thread died 2 years ago but since I had the same problem and I thought it was difficult to find a solution I'll still write here something我知道这个帖子在 2 年前就死了,但由于我遇到了同样的问题,我认为很难找到解决方案,我仍然会在这里写点东西
The issue for me was the folder where the .exe was.我的问题是 .exe 所在的文件夹。 It was in a random folder so it did not have permission to write on files.
它位于一个随机文件夹中,因此它没有写入文件的权限。
I solved this by moving to \\Appdata\\Roaming the folder with the build.我通过移动到 \\Appdata\\Roaming 文件夹来解决这个问题。 After doing this the program runs.
执行此操作后,程序运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.