# read no. of requests
if(os.path.isfile("config.txt")):
with open("config.txt", "r") as json_file:# Open the file for reading
configurations = json.load(json_file) # Read the into the buffer
# info = json.loads(js.decode("utf-8"))
print("read config file")
if("http_requests_count" in configurations.keys()):
print("present")
print(configurations["http_requests_count"])
number_of_requests = int(configurations["http_requests_count"])
print(number_of_requests)
我正在读取的config.txt文件
{
"first_container_ip": "8100",
"master_db" : "abc",
"http_requests_count" : "8",
"master_name" : "master",
"slave_names" : ["slave1", "slave2", "slave3"]
}
在代码的后面,当我打开配置文件编写它的giving me错误时,如
io.UnsupportedOperation: not readable
当我手动打开配置文件时,我发现它是空的
在完整的代码示例中,您可以
哪个将失败(无法从为写入而打开的文件中读取)并截断文件(就像使用
w
打开文件一样)这就是为什么会出现UnsupportedOperation错误,以及文件最终为空的原因
我建议进行重构,这样您就可以使用两个简单的函数来读取和写入配置文件:
(顺便说一下,既然您正在做DOCKER容器管理,请考虑将容器标签本身用作您的状态管理的主数据,而不是单独的文件,它可以很容易地脱离同步。)
相关问题 更多 >
编程相关推荐