Pickle:找不到文件或目录

时间:2016-10-16 11:07:24

标签: python numpy pickle

当我执行下面的代码时,我收到错误

import pickle
import numpy as np
from random import gauss

path ='/flash/data/'
a = [gauss(1.5, 2) for i in range(1000)]
pkl_file = open(path +'data.pkl', 'w')

%time pickle.dump(a, pkl_file)

我收到以下错误(即使我创建了/ flash / data /)

IOError Traceback (most recent call last)

<ipython-input-4-ac470dd231a6> in <module>()

      1 import pickle

----> 2 pkl_file = open (path + 'data.pkl','w')

      3 get_ipython().magic(u'time pickle.dump(a,pkl_file)')

IOError: [Errno 2] No such file or directory: '/flash/data/data.pkl'

2 个答案:

答案 0 :(得分:1)

这本书是个错误。路径应为:path ='。/ flash / data /'。一个 ”。”错过了。

答案 1 :(得分:1)

python 3的修正应该如下,

import os
os.makedirs(path, exist_ok=True)     #create directory when non-existing
path = './flash/data/'               #do not miss the '.'
import numpy as np
from random import gauss
a = [gauss(1.5, 2) for i in range(1000000)]
import pickle
pkl_file =  open(path+'.data.pkl','wb')     #use 'wb' instead of 'w' to avoid TypeError

%time pickle.dump(a, pkl_file)