1. 程式人生 > >python 之報錯:TypeError: write() argument must be str, not bytes

python 之報錯:TypeError: write() argument must be str, not bytes

在用 pickle.dump() 儲存二進位制檔案時,一直報錯,程式如下:

with open(os.path.join(FLAGS.train_data_dir, 'files.log'), 'w') as f:
   pickle.dump([training_paths, testing_paths], f)

查而得知,因為儲存方式預設是二進位制方式,故採用二進位制方式開啟。改為如下程式:

with open(os.path.join(FLAGS.train_data_dir, 'files.log'), 'wb+') as f:
    pickle.dump([training_paths, testing_paths], f)