By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

uploading files to file system/zipping/downloading problems

P: n/a
I am currently uploading a file from a users computer to the file
system on my server using python, just reading the file and writing the
binaries.

total_data=' '
while True:
data = upload_file.file.read(8192)
if not data:
break
total_data += data
f = open(target_file_name, 'wb')
f.write(total_data)
f.close

However when i download the file from the server it is not intact and
it cannot be opened. It is happening with every type of file.

It seemed to be working before and now it is..maybe I goofed up and
deleted something.
However I can't seem to find it.

any ideas??

Aug 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sun, 20 Aug 2006 13:49:22 -0700, OriginalBrownster wrote:
I am currently uploading a file from a users computer to the file
system on my server using python, just reading the file and writing the
binaries.

total_data=' '
while True:
data = upload_file.file.read(8192)
if not data:
break
total_data += data
f = open(target_file_name, 'wb')
f.write(total_data)
f.close

However when i download the file from the server it is not intact and
it cannot be opened. It is happening with every type of file.

It seemed to be working before and now it is..maybe I goofed up and
deleted something.
However I can't seem to find it.

any ideas??
You are re-creating, and overwriting, your file every time you go into
that while loop.

Aug 20 '06 #2

P: n/a
Assuming your upload_file.file.read() function works, the overwriting
should be the only issue. Just want to point out that total_data +=
data is not advisable for this example (speed/efficiency issue,
although i'm sure it could probably be even faster than what I replace
it with if u decided to use arrays, but it's probably not worth it XD),
and I think open() was replaced by file(), although they're probably
interchangable.

new code
-----------------
total_data = ' '
temp_data = []
while True:
data = upload_file.file.read(8192)
if not data:
break
temp_data.append(data)
total_data = ' '.join(temp_data)
somefile = file(target_file_name, 'wb')
somefile.write(total_data)
f.close()
-------------------

OriginalBrownster wrote:
I am currently uploading a file from a users computer to the file
system on my server using python, just reading the file and writing the
binaries.

total_data=' '
while True:
data = upload_file.file.read(8192)
if not data:
break
total_data += data
f = open(target_file_name, 'wb')
f.write(total_data)
f.close

However when i download the file from the server it is not intact and
it cannot be opened. It is happening with every type of file.

It seemed to be working before and now it is..maybe I goofed up and
deleted something.
However I can't seem to find it.

any ideas??
Aug 21 '06 #3

P: n/a
OriginalBrownster wrote:
I am currently uploading a file from a users computer to the file
system on my server using python, just reading the file and writing the
binaries.

total_data=' '
while True:
data = upload_file.file.read(8192)
if not data:
break
total_data += data
f = open(target_file_name, 'wb')
f.write(total_data)
f.close

However when i download the file from the server it is not intact and
it cannot be opened. It is happening with every type of file.

It seemed to be working before and now it is..maybe I goofed up and
deleted something.
However I can't seem to find it.

any ideas??
Two problems:

First, If you start total_data with a single space (as it shows in your
posted code). Then the output file has a space prepended to the
file's contents and that is NOT what you probably wanted.

Secondly, You are overwriting the files contents every time through
the loop. Your open, would need to be outside the loop (above the
while) and your close should be outside the loop also. Something more
like:

total_data=''
f = open(target_file_name, 'wb')

while True:
data = upload_file.file.read(8192)
if not data:
break
total_data += data
f.write(total_data)

f.close

You should take a look at shutil module. It is copyfile method
that makes your code must simpler (and faster I'm sure).

do

import shutl
help(shutil.copyfile)

import shutil
shutil.copyfile(uload_file_name, target_file_name)
-Larry Bates
Aug 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.