python - 卡住的Python文件无法访问 "Save_File"

标签 python

打包我的程序后,我决定对其进行测试以确保其正常工作,发生了一些事情,但主要问题在于 Save_File。

我使用 Save_File.py 来保存数据,静态保存数据。但是,卡住的 python 文件无法对该文件执行任何操作。它无法对其进行写入或读取。写入表示保存成功,但加载时会将所有值再次重置为零。

任何 .py 文件执行此操作是否正常?

这是 pyinstaller 的问题吗?

卡住过程错误?

或者是否有其他原因导致卡住文件无法写入、读取或与不在其中的文件交互? (Save_File 被卡住在内部并且不起作用,但删除它会导致错误,类似于它从未存在过)。

因此 exe 无法看到自身外部或自身内部发生变化...

编辑:添加了最基本版本的保存文件,但基本上,它被删除和重写了很多。

def save():
with open("Save_file.py", "a") as file:
    file.write("healthy = " + str(healthy) + "\n")
    file.write("infected = " + str(infected) + "\n")
    file.write("zombies = " + str(zombies) + "\n")
    file.write("dead = " + str(dead) + "\n")
    file.write("cure = " + str(cure) + "\n")
    file.write("week = " + str(week) + "\n")
    file.write("infectivity = " + str(infectivity) + "\n")
    file.write("infectivity_limit = " + str(infectivity_limit) + "\n")
    file.write("severity = " + str(severity) + "\n")
    file.write("severity_limit = " + str(severity_limit) + "\n")
    file.write("lethality = " + str(lethality) + "\n")
    file.write("lethality_limit = " + str(lethality_limit) + "\n")
    file.write("weekly_infections = " + str(weekly_infections) + "\n")
    file.write("dna_points = " + str(dna_points) + "\n")
    file.write("burst = " + str(burst) + "\n")
    file.write("burst_price = " + str(burst_price) + "\n")
    file.write("necrosis = " + str(necrosis) + "\n")
    file.write("necrosis_price = " + str(necrosis_price) + "\n")
    file.write("water = " + str(water) + "\n")
    file.write("water_price = " + str(water_price) + "\n")
    file.write("air = " + str(air) + "\n")
    file.write("blood = " + str(blood) + "\n")
    file.write("saliva = " + str(saliva) + "\n")
    file.write("zombify = " + str(zombify) + "\n")
    file.write("rise = " + str(rise) + "\n")
    file.write("limit = int(" + str(healthy) + " + " + str(infected) + " + " + str(dead) + " + " + str(zombies) + ")\n")
    file.write("old = int(1)\n")
Clear.clear()
WordCore.word_corex("SAVING |", "Save completed successfully")
time.sleep(2)
Clear.clear()
player_menu()

最佳答案

这可能是因为文件的卡住版本(.zip 文件中的某个位置)已加载,而从未加载您正在编写的版本(在文件未卡住时有效)

这是不好的做法:

- have a zillion global variables to hold your persistent data
- generate code in a python file just to evaluate it back again (it's _self-modifying code_).

如果您使用 C 或 C++ 语言,您会生成一些代码来存储数据,然后将其编译到新的可执行文件中吗?你会声明 300 个全局变量吗?我不这么认为。

您最好使用 json 数据格式和变量字典,这适用于卡住或不卡住:

你的字典会是这样的:

variables = {"healthy" : True, "zombies" : 345}  # and so on

访问您的变量:

if variables["healthy"]:  # do something

然后保存函数:

import json
def save():
    with open("data.txt", "w") as file:
        json.dump(variables,file,indent=3)

创建一个包含如下数据的文本文件:

{
   "healthy": true,
   "zombies": 345
}

和加载函数(将变量声明为全局以避免创建相同的变量,但仅限本地)

def load():
   global variables
   with open("data.txt", "r") as file:
       variables = json.load(file)

关于python - 卡住的Python文件无法访问 "Save_File",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53802042/

相关文章:

python - 获取图像内部特征并去除边界

python - 如何在 TensorFlow 图中添加 if 条件?

python - 如何更改 Powershell 中的路径?

Python 在 PyCharm 中工作,但在终端中不工作

python - 如何更新 Graphite 烯mongo python中EmbeddedDocument的字段

python - 改变随机森林中每棵树的权重

python - numpy 数组索引的意外行为

python - 在 Google App Engine 上使用 python 使用 POST 方法处理 HTML 表单数据

python - 使用 .replace 和 .strip 的更好方法? Python

python - 类参数和返回值的类型注释