Forums

not able to open pickle file in flask application.

I have created a flask application which uses pickle. now when i am trying to open that pickle file it gives file or directory not fond error. the file is present in same directory where all my files are present. can someone help me out. the directory structure: home/nih****/mysite

classi = open('classi1.pickle', 'rb')

classifier = pickle.load(classi)

classi.close()

FileNotFoundError: [Errno 2] No such file or directory: 'classi1.pickle'

Don't use a relative path if you have no idea what the working directory is. Use an absolute path then you'll know that you're accessing the correct file.

Thank you for such a quick response but i didnt get what do you mean by relative and absolute path. i am new in this stuff. I also tried this solution but didnt worked out.


import os

my_dir = os.path.dirname( file)

pickle_file_path = os.path.join(my_dir, 'classi1.pickle')

with open(pickle_file_path, 'rb') as pickle_file:

classifier = pickle.load(pickle_file)

It gives ERROR.

classifier = pickle.load(pickle_file)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xae in position 3: ordinal not in range(128)

That's exactly what I meant. The code is now finding the file correctly, but there is something wrong with your pickle or with the way you're reading it. Are you using the same protocol? It's also possible that for certain things, different versions of Python or Pythons from different machines may produce pickles that are incompatible.

on my local machine i have python 2.7.11 here we have 2.7.6. does that matter?

It could, but I'm not sure. In general, I never use pickles to transport data - they're mostly useful for storing information between different runs of a program in the same environment.

Actually i am using pickle to store my Machine learning model which i have'd trained. Anyways thanks for help glenn