safest way to open files on all platforms

Discussion in 'Python' started by rbt, Feb 14, 2005.

  1. rbt

    rbt Guest

    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
     
    rbt, Feb 14, 2005
    #1
    1. Advertisements

  2. "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>
     
    Fredrik Lundh, Feb 14, 2005
    #2
    1. Advertisements

  3. rbt

    rbt Guest

    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.
     
    rbt, Feb 14, 2005
    #3
  4. "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>
     
    Fredrik Lundh, Feb 14, 2005
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johan Bergman

    Is this the optimal FIR filter on all platforms?

    Johan Bergman, Oct 25, 2003, in forum: C Programming
    Replies:
    25
    Views:
    880
    Johan Bergman
    Oct 27, 2003
  2. Replies:
    7
    Views:
    1,784
  3. Joseph Turian
    Replies:
    2
    Views:
    400
    Daniel Bickett
    Jul 27, 2005
  4. H5N1
    Replies:
    0
    Views:
    682
  5. William Payne
    Replies:
    13
    Views:
    984
    Dave Thompson
    Sep 8, 2004
  6. Aussie Rules

    safest way to pass parameters between pages

    Aussie Rules, Oct 9, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    725
    Steven Cheng[MSFT]
    Oct 9, 2007
  7. Giampaolo Rodola'

    Is os.lstat available on all platforms?

    Giampaolo Rodola', Nov 29, 2007, in forum: Python
    Replies:
    2
    Views:
    331
    Giampaolo Rodola'
    Nov 29, 2007
  8. J. Shane Kunkle

    Safest way to pass data between web apps?

    J. Shane Kunkle, Mar 11, 2005, in forum: ASP .Net Security
    Replies:
    4
    Views:
    672
    Paul Glavich [MVP ASP.NET]
    Mar 14, 2005
Loading...