Search code examples
pythonloggingfile-writingpprint

How to pprint globals() in a loop (that has another write log command) at end of file?


I am trying to create a log file that receives variable information using pprint globals() and writes to file. But since I have to use many loops, is there a way to place all the pprint globals() output during each loop at the end of log file for the code shown below:

import numpy as np
from pprint import pprint
A = np.array([1, 2, 3, 4])
f = open("log.txt", 'w')
n = 3
for i in range(n):
    f.write(u'\u27A4 - %s\n'.encode('utf-8') % str(i))
    A = A + 1
    f.writelines(list(u'    \u27B3 - %s\n'.encode('utf-8') % i for i in A))
    pprint(globals(), f)
f.close()

Output

➤ - 0
    ➳ - 2
    ➳ - 3
    ➳ - 4
    ➳ - 5
{'A': array([2, 3, 4, 5]),
 '__builtins__': <module '__builtin__' (built-in)>,
 '__doc__': None,
 '__file__': '~/Stack exchange/pprint_global.py',
 '__name__': '__main__',
 '__package__': None,
 'f': <open file 'log.txt', mode 'w' at 0xb66588b8>,
 'i': 0,
 'n': 2,
 'np': <module 'numpy' from '/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'>,
 'pprint': <function pprint at 0xb6e2748c>}
➤ - 1
    ➳ - 3
    ➳ - 4
    ➳ - 5
    ➳ - 6
{'A': array([3, 4, 5, 6]),
 '__builtins__': <module '__builtin__' (built-in)>,
 '__doc__': None,
 '__file__': '~/Stack exchange/pprint_global.py',
 '__name__': '__main__',
 '__package__': None,
 'f': <open file 'log.txt', mode 'w' at 0xb66588b8>,
 'i': 1,
 'n': 2,
 'np': <module 'numpy' from '/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'>,
 'pprint': <function pprint at 0xb6e2748c>}

Desired output

➤ - 0
    ➳ - 2
    ➳ - 3
    ➳ - 4
    ➳ - 5
➤ - 1
    ➳ - 3
    ➳ - 4
    ➳ - 5
    ➳ - 6
{'A': array([2, 3, 4, 5]),
 '__builtins__': <module '__builtin__' (built-in)>,
 '__doc__': None,
 '__file__': '~/Stack exchange/pprint_global.py',
 '__name__': '__main__',
 '__package__': None,
 'f': <open file 'log.txt', mode 'w' at 0xb66588b8>,
 'i': 0,
 'n': 2,
 'np': <module 'numpy' from '/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'>,
 'pprint': <function pprint at 0xb6e2748c>}
{'A': array([3, 4, 5, 6]),
 '__builtins__': <module '__builtin__' (built-in)>,
 '__doc__': None,
 '__file__': '~/Stack exchange/pprint_global.py',
 '__name__': '__main__',
 '__package__': None,
 'f': <open file 'log.txt', mode 'w' at 0xb66588b8>,
 'i': 1,
 'n': 2,
 'np': <module 'numpy' from '/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'>,
 'pprint': <function pprint at 0xb6e2748c>}

The first impression would be to place the pprint command at the end of the code, but this would not give me the variable information during each loop. It would give me the value only at the end of the loop. In short I need variable information at each loop (which has another write log command) written to the end of the log file.


Solution

  • An easy way to do this would be to write the outputs to two separate text files and then concatenate the files in the code itself. You could choose the order in which to concatenate the files also, by changing the log_file_list. With your code using the above method:

    import numpy as np
    from pprint import pprint
    A = np.array([1, 2, 3, 4])
    f1 = open("log1.txt", 'w')
    f2 = open("log2.txt", 'w')
    n = 2
    for i in range(n):
        f1.write(u'\u27A4 - %s\n'.encode('utf-8') % str(i))
        A = A + 1
        f1.writelines(list(u'    \u27B3 - %s\n'.encode('utf-8') % i for i in A))
        pprint(globals(), f2)
    f1.close()
    f2.close()
    log_file_list = ['log1.txt', 'log2.txt']
    with open('log.txt', 'w') as output_file:
        for log_file in log_file_list:
            with open(log_file) as input_file:
                for line in input_file:
                    output_file.write(line)
            output_file.write('\n')
    

    Alternate method

    Alternate code that does not use lines:

    import numpy as np
    from pprint import pprint
    A = np.array([1, 2, 3, 4])
    f1 = open("log1.txt", 'w')
    f2 = open("log2.txt", 'w')
    n = 2
    for i in range(n):
        f1.write(u'\u27A4 - %s\n'.encode('utf-8') % str(i))
        A = A + 1
        f1.writelines(list(u'    \u27B3 - %s\n'.encode('utf-8') % i for i in A))
        pprint(globals(), f2)
    f1.close()
    f2.close()
    f1 = open("log1.txt", 'r')
    f2 = open("log2.txt", 'r')
    f = open("log.txt", 'w')
    f.write(f1.read())
    f.write(f2.read())
    f.close()