safest way to open files on all platforms

rbt rbt at athop1.ath.vt.edu
Mon Feb 14 09:32:55 EST 2005


Fredrik Lundh wrote:
> "rbt" wrote:
> 
> 
>>I believe that this is the safest way to open files on Windows, Linux, Mac and Unix, but I wanted 
>>to ask here just to be sure:
>>
>>fp = file('filename', 'rb')
>>
>>The 'b' on the end being the most important ingredient (especially on Windows as a simple 'r' on a 
>>binary file might cause some sort of corruption).
>>
>>Anyway, am I right in saying this? That 'rb' is the safest way to open files for reading and that 
>>it should work well on *all* Python supported platforms?
> 
> 
> "rb" works on all platforms, yes.  but it doesn't work well if you're reading a text file.
> 
> (when reading text files, the "U" option may also be useful.  see doc for details)
> 
> </F> 

I'm using 'rb' in a situation where all files on the drive are opened. 
I'm not checking how the file is encoded before opening it (text, 
unicode, jpeg, etc.) That's why I though 'rb' would be safest.

Can 'U' be used with 'rb'? Should it be? From what I read, 'U' handles 
the different ways in which the OS handles the 'end of line' on text 
files, but other than that, I don't think it's useful for me.



More information about the Python-list mailing list