Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > safest way to open files on all platforms

Reply
Thread Tools

safest way to open files on all platforms

 
 
rbt
Guest
Posts: n/a
 
      02-14-2005
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?

Many thanks,

RBT
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      02-14-2005
"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>



 
Reply With Quote
 
 
 
 
rbt
Guest
Posts: n/a
 
      02-14-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.
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      02-14-2005
"rbt" wrote:

> 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.


if "safest way to open files" meant "safest way to open binary files", why
didn't you say so in your first post?

> Can 'U' be used with 'rb'? Should it be?


"U" is for text files, "b" is for binary files. binary files contain bytes, text
files contain text. if you're opening a file to read it as text (readline, read-
lines, iteration, etc), use "r" or "rU". if you're opening a file to read it as
binary bytes (read), use "rb".

</F>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
the safest way of handling datetime between asp.net and ms sql H5N1 ASP .Net 0 10-09-2006 01:47 AM
Firefox, Mozilla, Netscape, not the safest way to go [STICKY] Uplink Computer Information 6 12-23-2005 07:14 PM
Safest Way to Share Files? =?Utf-8?B?THVpcw==?= Wireless Networking 3 11-21-2005 05:07 PM
safest way to kill a thread martinnitram@excite.com Python 7 01-20-2005 02:40 AM
Safest way to clean up registry??? Bud Light Computer Support 7 12-26-2004 02:56 AM



Advertisments