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.