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

dpapathanasiou denis.papathanasiou at gmail.com
Mon Dec 7 12:12:50 EST 2009


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




More information about the Python-list mailing list