python pickle 写入与读取

from os import walk

train_names = []
for (dirpath, dirnames, filenames) in walk('/train_cover'):
    train_names.extend(filenames)
    break

test_names = []
for (dirpath, dirnames, filenames) in walk('/test_cover'):
    test_names.extend(filenames)
    break

with open('./train.txt', 'a+') as w:
    for name in train_names:
        w.write(name + '\n')

with open('./val.txt', 'a+') as w:
    for name in test_names:
        w.write(name + '\n')

import pickle
import pickle
with open('val.p', 'wb') as f:
    pickle.dump(test_names, f)


with open('val.p', 'rb') as f:
    IL_val = pickle.load(f)
    print(IL_val)

你可能感兴趣的:(python)