Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pickle a empty file?

Tags:

python

I want to pickle a file that sometime's is empty. Right now its empty, but my idea is that its going to grow over time.

How do i check if a file is "pickable" since it seems that you can not pickle a empty file?

like image 687
Jason94 Avatar asked Jun 30 '10 11:06

Jason94


People also ask

Does pickle load empty file?

1 Answer. Show activity on this post. pickle doesn't work that way: an empty file doesn't create an empty list.

What is pickle dump ()?

Python Pickle dump dump() function to store the object data to the file. pickle. dump() function takes 3 arguments. The first argument is the object that you want to store. The second argument is the file object you get by opening the desired file in write-binary (wb) mode.

Which method is used to pickle a file?

To use pickle to serialize an object, we use the pickle. dump function, which takes two arguments: the first one is the object, and the second argument is a file object returned by the open function. Note here the mode of the open function is 'wb' which indicates write binary file.


1 Answers

Simply use a try/except block.

def example():
  try:
    return pickle.loads("")
  except EOFError:
    return None

It's easier to ask forgiveness than permission. :)

like image 183
badp Avatar answered Oct 18 '22 04:10

badp