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

D

dpapathanasiou

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
 
J

Joe Riopel

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

Ulrich Eckhardt

dpapathanasiou said:
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
 
L

Lie Ryan

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top