[Tutor] How do I fix this Invalid Mode?

Dan Lowe dan at tangledhelix.com
Sat Dec 17 20:39:16 CET 2005


On Dec 17, 2005, at 2:00 PM, Nathan Pinno wrote:

> Here is the latest error:
> The Currency Exchange Program
> By Nathan Pinno
>
> Traceback (most recent call last):
>   File "D:\Python24\exchange.py", line 27, in -toplevel-
>     store = open('exch.txt', 'b')#load
> IOError: invalid mode: b

[snip...]

> store = open('exch.txt', 'b')#load
> exch = pickle.load(store)
> store.close()

It looks like what you are trying to do is read exch.txt as a binary  
file. The problem is that 'b' by itself is not a valid mode; it's a  
modifier to one of the other modes (r, w or a).

So to read a file in binary mode, what you want to do is:

	store = open('exch.txt', 'rb')

Using your own code as an example, see how you implemented the save  
feature (option 9). That open() correctly uses the 'wb' flag to write  
the file in binary mode.

     elif menu_option == 9:
         store = open("exch.txt", 'wb') #save
         pickle.dump(exch, store)
         store.close()
         break

  -dan

-- 
Black holes are where God divided by zero.  -Steven Wright


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051217/da5b0b4a/attachment.htm


More information about the Tutor mailing list