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)

猜你喜欢

转载自blog.csdn.net/weixin_42338058/article/details/84593464
今日推荐