0

I have a list containing many tuples,and stored in streaming_cfg

and tried to dump to a text file DEBUG_STREAMING_CFG_FILE

however it is an empty file contains nothing. why ?

    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')]
newBike
  • 14,385
  • 29
  • 109
  • 192

3 Answers3

2

You are not actually calling close, you have merely an expression that evaluates to the callable object.

Replace the last line by

debug_file.close()

By the way, mistakes like this can be prevented in modern python by use of context managers:

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')
filmor
  • 30,840
  • 6
  • 50
  • 48
  • Out of curiosity---when the reference counter goes down to zero, or the interpreter shuts down, does the interpreter call `close()` on each `file` object? (Maybe the OP invoked `exit(2)` to shutdown the interpreter.) – nodakai Feb 07 '14 at 10:35
  • http://stackoverflow.com/questions/1834556/does-a-file-object-automatically-close-when-its-reference-count-hits-zero – filmor Feb 07 '14 at 12:29
  • And if you call `exit` you are at your `libc`'s mercy, I'd guess. – filmor Feb 07 '14 at 12:30
  • I take that last one back, to be that destructive (i.e. not call your objects' destructors) you have to use `os._exit`, in which case your open files' buffers won't be flushed. – filmor Feb 07 '14 at 12:32
  • @filmore Thanks for your info. Now I wonder why the OP had the problem just because he failed to properly call `close()`. It isn't a very productive speculation, though – nodakai Feb 07 '14 at 12:53
  • Well, it is a productive speculation IMHO ;) My current guess is that the program was still running while the debug file was read. – filmor Feb 07 '14 at 13:37
  • I misuse the syntax with ruby and python ~~ sorry – newBike Feb 08 '14 at 02:06
0

Modern 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)

No need to close the opened file.

0

You didn't call close(), but if you use a simpler with clause, you don't have to either:

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')
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395