0

So I'm trying to create a file and read from it even though its empty. So the next time I run the program the file will already exist and there will be data in it.

#this should create the file
filename = open("roombooking.bin","wb")
#this should load the data into a list but since its first created it should be an empty list
monday=pickle.load(open("roombooking.bin","rb"))

this is the error i get

Traceback (most recent call last):
    monday=pickle.load(open("roombooking.bin","rb"))
  File "C:\Python27\lib\pickle.py", line 1378, in load
    return Unpickler(file).load()
  File "C:\Python27\lib\pickle.py", line 858, in load
    dispatch[key](self)
  File "C:\Python27\lib\pickle.py", line 880, in load_eof
    raise EOFError
EOFError
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
lun
  • 45
  • 1
  • 8
  • Hearing this, I start wondering if the data that "will be" in the file "next time" will also cause problems. You do understand you can only use `pickle` (or anything else that fully conforms to its [Data stream format](https://docs.python.org/2/library/pickle.html#data-stream-format)) to write there and that it's not secure against malicious data? – ivan_pozdeev Oct 21 '14 at 15:41
  • Note that `open()` returns a `file` object, not a filename -- and also that opening the file with mode `"wb"` will overwrite the contents of any existing file effectively emptying it. – martineau Oct 21 '14 at 16:07
  • @ivan yeah its okay its for an assignment. – lun Oct 21 '14 at 17:25
  • @martineau any suggestions on how to apporach it then? – lun Oct 21 '14 at 17:26
  • 1
    Trying reading it first, as shown in David Robinson's answer -- you could also create the file in the `except EOFError:` clause. Without more information about what you're doing, it's difficult to suggest how to go about creating it in the first place. Note an alternative approach would be to test for the existence of the file before attempting open it by using [`os.path.exists()`](https://docs.python.org/2/library/os.path.html?highlight=exists#os.path.exists). This idiom is known as the "look before you leap" ([LBYL](http://stackoverflow.com/questions/404795/lbyl-vs-eafp-in-java)) approach. – martineau Oct 21 '14 at 19:13
  • yeah thank you his apporach works but it overwrites the file i might just use an if statement for existence of file first as u suggested. – lun Oct 21 '14 at 20:26
  • @martineau, @user3312428 note that LBYL approach is vulnerable to race condition if something can change between the check and the operation. If this is the case, it's better to use an atomic operation like `open("r+b")` or the aforementioned EAFP approach. – ivan_pozdeev Oct 22 '14 at 10:19
  • @ivan_pozdeev: While in theory LBYL can lead to a [race condition](https://en.wikipedia.org/wiki/Race_condition#Software), in reality it's very unlikely in a single-threaded program. That said, it's completely possible to avoid overwriting the file utilizing EAFP approach part outlined in [Robinson's answer](http://stackoverflow.com/a/26490512/355230). – martineau Oct 22 '14 at 13:10

1 Answers1

5

pickle doesn't work that way: an empty file doesn't create an empty list.

An empty list would look like this in a pickle file:

(lp0
.

If you would like to handle it so that an empty file leads to an empty list, you can use a try/except block:

try:
    monday=pickle.load(open("roombooking.bin","rb"))
except EOFError:
    monday = []

Do note, however, that "the next time you run the program", if the line filename = open("roombooking.bin","wb") occurs, it will overwrite the file.

David Robinson
  • 77,383
  • 16
  • 167
  • 187