从 .py 文件转换为 .exe(使用 py2exe),如何从写入文件导入数据?
我的程序将数据保存到外部 .py 文件以供稍后使用。这通常工作正常,但是一旦转换为可执行文件,它会在外部保存文件,但仅从内部数据中提取。
代码如下所示:
def save_game():
with open("save.py") as _save:
_save.write("""variables to be used later""")
_save.close()
def load_game():
import save
reload(save)
x = save.x
在 .py 中,它创建 save.py 并写入所有变量。加载时,所有变量都会完全按照写入方式导入。
创建 .exe 后,它会使用所有变量创建 save.py,但它仅使用创建 .exe 时存在的 save.py 的迭代。
将我的应用程序转换为可执行文件后,有什么方法可以实现类似的功能吗?
My program saves data to an external .py file for use later on. This works fine normally, however once converted to an executable, it saves a file externally, but only draws from the internal data.
This is what the code looks like:
def save_game():
with open("save.py") as _save:
_save.write("""variables to be used later""")
_save.close()
def load_game():
import save
reload(save)
x = save.x
In the .py, it creates save.py and writes all the variables. When it loads, all of the variables are imported exactly as written.
After the .exe was created, it creates save.py with all the variables, but it only uses the iteration of save.py that existed when the .exe was created.
Is there any way to achieve similar functionality after converting my app to an executable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用官方方法并使用
pickle
代替。Use the official method and use
pickle
instead.