Use of sys.modules
and setattr
will work like charm:
import pickle
from sys import modules
from os import listdir
main_script = modules['__main__']
for file in [path for path in listdir() if path.endswith('pk1')]:
with open(file, "rb") as fp:
setattr(main_script, fp.name.split('.')[0], pickle.load(fp))
print(a, b, c, d, e, sep='\n')
code used to generate sample files - a~e.pk1:
import pickle
for i, n in enumerate('abcde'):
with open(f"{n}.pk1", "wb") as fp:
pickle.dump([*iter(range(i))], fp)
output:
[]
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
But would be nice if you convert those pickles
to json
as pickled file are dependent to python versions, and are potential threat.
Use this to convert all .pk1
in directory:
import json
import pickle
from os import listdir
for file in [path for path in listdir() if path.endswith('pk1')]:
with open(file, "rb") as fp:
with open(file.split('.')[0] + ".json", "wt") as fp_json:
json.dump(pickle.load(fp), fp_json)