Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么ConfigParser不立即写入磁盘?_Python_Configparser - Fatal编程技术网

Python 为什么ConfigParser不立即写入磁盘?

Python 为什么ConfigParser不立即写入磁盘?,python,configparser,Python,Configparser,给定以下脚本: import ConfigParser from datetime import datetime import time def write_stuff(): section = "test" item = "oh hey there" conf_filename = "test.conf" conf = ConfigParser.ConfigParser() conf.readfp(open(conf_filename, 'r',

给定以下脚本:

import ConfigParser
from datetime import datetime
import time

def write_stuff():
    section = "test"
    item = "oh hey there"
    conf_filename = "test.conf"

    conf = ConfigParser.ConfigParser()
    conf.readfp(open(conf_filename, 'r', 0))

    timestamp = datetime.now().strftime("%Y-%m-%d_%H%M%S")

    conf.set(section, timestamp, item)

    with open(conf_filename, "w", 0) as conf_file:
        # time.sleep(1)
        conf.write(conf_file)

write_stuff()
write_stuff()
write_stuff()
write_stuff()
它将只向文件写入一个条目,如下所示:

$ touch test.conf
$ python tests.py  # this is what I've named the above
$ $ cat test.conf
[test]
2012-10-10_231439 = oh hey there
但是,如果取消对time.sleep(1)的注释,则会显示所有条目。奇怪的是(无论如何,对我来说),如果您有一个编写_stuff()的调用,并且从shell中快速连续地调用脚本,甚至会发生这种情况。我认为,一旦Python退出,任何将要进入磁盘的内容都将进入磁盘。发生什么事了


环境:Mac OS X 10.8上的Python 2.7.3

如果要反复编写相同的条目,请使用
“a”
而不是
“w”
附加文件:

with open("test.txt", "a") as myfile:
    myfile.write("appended text")
Mybe如果您想要类似的内容,则该部分将打印一次,您可以向其中添加多个项目:

config.add_section('Section1')
config.set('Section1', 'an_int', '15')
config.set('Section1', 'a_bool', 'true')
config.set('Section1', 'a_float', '3.1415')
config.set('Section1', 'baz', 'fun')
config.set('Section1', 'bar', 'Python')
config.set('Section1', 'foo', '%(bar)s is %(baz)s!')

# Writing our configuration file to 'example.cfg'
with open('example.cfg', 'wb') as configfile:
    config.write(configfile)
输出:

[Section1]
bar = Python
baz = fun
a_bool = true
an_int = 15
foo = %(bar)s is %(baz)s!
a_float = 3.1415

如您所见,您应该在一次写入中完成此操作,而无需多次调用函数。

如果要反复写入相同的条目,请使用
“a”
而不是
“w”
来追加文件:

with open("test.txt", "a") as myfile:
    myfile.write("appended text")
Mybe如果您想要类似的内容,则该部分将打印一次,您可以向其中添加多个项目:

config.add_section('Section1')
config.set('Section1', 'an_int', '15')
config.set('Section1', 'a_bool', 'true')
config.set('Section1', 'a_float', '3.1415')
config.set('Section1', 'baz', 'fun')
config.set('Section1', 'bar', 'Python')
config.set('Section1', 'foo', '%(bar)s is %(baz)s!')

# Writing our configuration file to 'example.cfg'
with open('example.cfg', 'wb') as configfile:
    config.write(configfile)
输出:

[Section1]
bar = Python
baz = fun
a_bool = true
an_int = 15
foo = %(bar)s is %(baz)s!
a_float = 3.1415

如您所见,您应该在一次写入中完成此操作,而无需多次调用函数。

这里的问题是,您在配置文件中使用的键值是一个具有1秒分辨率的时间戳。这意味着,当您连续四次调用
write_stuff()
时,时间没有改变,时间戳没有改变,您只需覆盖以前的值,而不是添加新值

您需要做的是每次生成一个唯一的键值。如果您希望保留时间戳值,则可以这样做:

count = 0

def write_stuff():
    global count

    section = "test" 
    item = "oh hey there" 
    conf_filename = "test.conf" 

    conf = ConfigParser.ConfigParser() 
    conf.readfp(open(conf_filename, 'r', 0)) 

    timestamp = datetime.now().strftime("%Y-%m-%d_%H%M%S")+ "_%s" % count
    count += 1

    conf.set(section, timestamp, item) 

    with open(conf_filename, "w", 0) as conf_file: 
        conf.write(conf_file) 

请注意,写入配置文件的值不会按任何特定顺序排列。

这里的问题是,您在配置文件中使用的键值是具有1秒分辨率的时间戳。这意味着,当您连续四次调用
write_stuff()
时,时间没有改变,时间戳没有改变,您只需覆盖以前的值,而不是添加新值

您需要做的是每次生成一个唯一的键值。如果您希望保留时间戳值,则可以这样做:

count = 0

def write_stuff():
    global count

    section = "test" 
    item = "oh hey there" 
    conf_filename = "test.conf" 

    conf = ConfigParser.ConfigParser() 
    conf.readfp(open(conf_filename, 'r', 0)) 

    timestamp = datetime.now().strftime("%Y-%m-%d_%H%M%S")+ "_%s" % count
    count += 1

    conf.set(section, timestamp, item) 

    with open(conf_filename, "w", 0) as conf_file: 
        conf.write(conf_file) 

请注意,写入配置文件的值不会按任何特定顺序排列。

如果在追加模式下打开,则配置会被写入文件4次。。。不仅是项目,还有4个不同的部分。我不相信这是正确的解决方案。如果你使用write_stuff()4次,那么它肯定会被写入4次。。。你想达到什么目标?用你想要的结果编辑你的问题,现在很难理解。如果在附加模式下打开,那么配置会被写入文件4次。。。不仅是项目,还有4个不同的部分。我不相信这是正确的解决方案。如果你使用write_stuff()4次,那么它肯定会被写入4次。。。你想达到什么目标?用你想要的结果编辑你的问题,目前很难理解。