IO Gurus: what are the differences between these two methods?

Discussion in 'Python' started by dpapathanasiou, Dec 7, 2009.

  1. I have two methods for writing binaries files: the first works with
    data received by a server corresponding to a file upload, and the
    second works with data sent as email attachments.

    The odd thing is, they're not interchangeable: if I use the first one
    to saved data parsed from an email attachment, it fails; similarly,
    the second function fails when dealing with an uploaded file data.

    What are the critical differences?

    def write_binary_file (folder, filename, f, chunk_size=4096):
    """Write the file data f to the folder and filename combination"""
    result = False
    if confirm_folder(folder):
    try:
    file_obj = open(os.path.join(folder, file_base_name
    (filename)), 'wb', chunk_size)
    for file_chunk in read_buffer(f, chunk_size):
    file_obj.write(file_chunk)
    file_obj.close()
    result = True
    except (IOError):
    print "file_utils.write_binary_file: could not write '%s'
    to '%s'" % (file_base_name(filename), folder)
    return result

    def write_binary_file (folder, filename, filedata):
    """Write the binary file data to the folder and filename
    combination"""
    result = False
    if confirm_folder(folder):
    try:
    file_obj = open(os.path.join(folder, file_base_name
    (filename)), 'wb')
    file_obj.write(filedata)
    file_obj.close()
    result = True
    except (IOError):
    print "file_utils.write_binary_file: could not write '%s'
    to '%s'" % (file_base_name(filename), folder)
    return result
    dpapathanasiou, Dec 7, 2009
    #1
    1. Advertising

  2. dpapathanasiou

    Joe Riopel Guest

    On Mon, Dec 7, 2009 at 12:12 PM, dpapathanasiou
    <> wrote:
    > I have two methods for writing binaries files: the first works with
    > data received by a server corresponding to a file upload, and the
    > second works with data sent as email attachments.
    >
    > The odd thing is, they're not interchangeable: if I use the first one
    > to saved data parsed from an email attachment, it fails; similarly,
    > the second function fails when dealing with an uploaded file data.


    When you say it fails, are there any error messages displayed,
    exceptions raised, etc? I am not sure what you mean, in regards to
    "fails".
    Joe Riopel, Dec 7, 2009
    #2
    1. Advertising

  3. dpapathanasiou wrote:
    > I have two methods for writing binaries files: the first works with
    > data received by a server corresponding to a file upload, and the
    > second works with data sent as email attachments.


    Hmmm, no. Looking at your code, the first of your functions actually treats
    its argument as a stream, while the second one treats it like a byte buffer
    (as str object, to be precise).

    > The odd thing is, they're not interchangeable: if I use the first one
    > to saved data parsed from an email attachment, it fails; similarly,
    > the second function fails when dealing with an uploaded file data.


    There is nothing odd about that, they are different functions working with
    different things.

    > What are the critical differences?
    >
    > def write_binary_file (folder, filename, f, chunk_size=4096):

    [...]
    > for file_chunk in read_buffer(f, chunk_size):
    > file_obj.write(file_chunk)

    [...]
    > def write_binary_file (folder, filename, filedata):

    [...]
    > file_obj.write(filedata)


    BTW: You could have reduced the code yourself before posting. You might have
    seen the difference yourself then! Further, I'm curious, do you have any
    non-binary files anywhere? =)

    Uli

    --
    Sator Laser GmbH
    Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
    Ulrich Eckhardt, Dec 8, 2009
    #3
  4. dpapathanasiou

    Lie Ryan Guest

    On 12/8/2009 4:12 AM, dpapathanasiou wrote:
    > I have two methods for writing binaries files: the first works with
    > data received by a server corresponding to a file upload, and the
    > second works with data sent as email attachments.
    >
    > The odd thing is, they're not interchangeable: if I use the first one
    > to saved data parsed from an email attachment, it fails; similarly,
    > the second function fails when dealing with an uploaded file data.
    >
    > What are the critical differences?


    Those code reeks for refactoring. If you're lucky enough to be in python
    2.6 or above, use the io module to wrap the string in second function in
    a file-like object.
    Lie Ryan, Dec 8, 2009
    #4
    1. Advertising

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. John_Woo
    Replies:
    5
    Views:
    1,508
  2. JohnJSal
    Replies:
    13
    Views:
    424
    John Salerno
    Nov 4, 2006
  3. xz
    Replies:
    4
    Views:
    279
    James Kanze
    Mar 16, 2008
  4. Laser Lips
    Replies:
    2
    Views:
    140
    Laser Lips
    Feb 24, 2011
  5. iMath
    Replies:
    5
    Views:
    242
    Piet van Oostrum
    Sep 9, 2012
Loading...

Share This Page