我有一个包含许多元组的列表,并存储在streaming_cfg
并尝试转储到文本文件DEBUG_STREAMING_CFG_FILE
然而它是一个空文件,什么也没包含。 为什么?
debug_file = open(DEBUG_STREAMING_CFG_FILE,'w')
for lst in streaming_cfg:
print(lst)
debug_file.write(' '.join(str(s) for s in lst) + '\n')
debug_file.close
streaming_cfg
[('0', '0', 'h264', '1/4', '1280x1024', '10', 'vbr', '27', '8m'),
('0', '0', 'h264', '1/4', '1280x1024', '10', 'cbr', '6m', 'framerate'),
('0', '0', 'h264', '1/4', '1280x1024', '10', 'cbr', '6m', 'imagequality'),
('0', '0', 'h264', '1/4', '1280x1024', '10', 'cbr', '8m', 'framerate'),
('0', '0', 'h264', '1/4', '1280x1024', '10', 'cbr', '8m', 'imagequality'),
('0', '0', 'h264', '1/4', '2560x1920', '8', 'vbr', '27', '8m'),
('0', '0', 'h264', '1/4', '2560x1920', '8', 'cbr', '6m', 'framerate'),
('0', '0', 'h264', '1/4', '2560x1920', '8', 'cbr', '6m', 'imagequality'),
('0', '0', 'h264', '1/4', '2560x1920', '8', 'cbr', '8m', 'framerate'),
('0', '0', 'h264', '1/4', '2560x1920', '8', 'cbr', '8m', 'imagequality'),
('0', '0', 'mjpeg', '1/2', '1280x1024', '10', 'vbr', '25', '4m'),
('0', '0', 'mjpeg', '1/2', '1280x1024', '10', 'cbr', '6m', 'imagequality'),
('0', '0', 'mpeg4', '1/2', '1280x1024', '10', 'vbr', '28', '6m'),
('0', '0', 'mpeg4', '1/2', '1280x1024', '10', 'cbr', '3m', 'imagequality')]
答案 0 :(得分:2)
您实际上并没有调用close
,只有一个表达式可以计算可调用对象。
用
替换最后一行debug_file.close()
顺便说一句,使用context managers可以在现代python中防止这样的错误:
with open(DEBUG_STREAMING_CFG_FILE,'w') as debug_file:
for lst in streaming_cfg:
print(lst)
debug_file.write(' '.join(str(s) for s in lst) + '\n')
答案 1 :(得分:0)
现代Python:
with open(DEBUG_STREAMING_CFG_FILE, "w") as f:
for lst in streaming_cfg:
print(' '.join(str(s) for s in lst), file=f)
无需关闭已打开的文件。
答案 2 :(得分:0)
您没有调用close()
,但如果您使用更简单的with
子句,则不需要:
with open(DEBUG_STREAMING_CFG_FILE, 'w') as f:
for lst in streaming_cfg:
print(lst)
f.write(' '.join(str(s) for s in lst) + '\n')