Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileNotFoundError while writing to a Pickle file

Tags:

python

pickle

I'm taking all the filenames in a certain directory in a list, and would like to write this list in a pickle file. Here's the code I am using:

import _pickle as pickle
with open(filepath+'filenames.pkl', 'wb') as f:
    pickle.dump(filenames, f)

This gives me the following error:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-32-c59e6889d2fe> in <module>()
      1 import _pickle as pickle
----> 2 with open(dpath+'filenames.pkl', 'wb') as f:
      3     pickle.dump(filenames, f)

FileNotFoundError: [Errno 2] No such file or directory: '/data/train/filenames.pkl'

I am supposed to create this file. Why is this file expected already?

(I'm using Python 3.6.)

like image 472
Patthebug Avatar asked Mar 05 '17 03:03

Patthebug


People also ask

How do you write in pickles?

Pickling Files To open the file for writing, simply use the open() function. The first argument should be the name of your file. The second argument is 'wb' . The w means that you'll be writing to the file, and b refers to binary mode.

What is the format of pickle file?

The data format used by pickle is Python-specific.

How do I view the contents of a pickle file?

Use pickle. load() to read a pickle file Use the syntax pickle_file = open("file. txt", "rb") to assign pickle_file a file object that points to the data in file. txt .

How do I fix No such file or directory in Python?

The Python "FileNotFoundError: [Errno 2] No such file or directory" occurs when we try to open a file that doesn't exist in the specified location. To solve the error, move the file to the directory where the Python script is located if using a local path, or use an absolute path.


1 Answers

In your case most probably /data/train/ directory does not exist

I tried this code and got same error:

import pickle as pickle
filenames='sadasdas'
with open('/tmp/not_exist/filenames.pkl', 'wb') as f:
    pickle.dump(filenames, f)

output:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-7-6a22c2148796> in <module>()
      1 import pickle as pickle
      2 filenames='sadasdas'
----> 3 with open('/tmp/not_exist/filenames.pkl', 'wb') as f:
      4     pickle.dump(filenames, f)

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/not_exist/filenames.pkl'

You can handle this problem by making sure the directory exists before writing to file: Programatic approach to do the same is this

import os
filename = "/tmp/not_exist/filenames.pkl"
os.makedirs(os.path.dirname(filename), exist_ok=True)
data = 'sadasdas'
with open('/tmp/not_exist/filenames.pkl', 'wb') as f:
    pickle.dump(data, f)

ref: https://stackoverflow.com/a/12517490/3027854

like image 148
Vikash Singh Avatar answered Sep 22 '22 20:09

Vikash Singh