uploading files to file system/zipping/downloading problems

O

OriginalBrownster

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??
 
P

Pontus Ekberg

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.
 
J

Jordan

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()
 
L

Larry Bates

OriginalBrownster said:
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
 
D

Dennis Lee Bieber

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
Still not right... You are referencing f.close, not invoking
f.close()!

Also, that performs a write() on each pass of the loop... BUT as
"total_data" has the data of each pass concatenated to it, you get
(symbolically):

block1
block1block2
block1block2block3

Better would be to remove "total_data" completely, and just use

f.write(data)

.....

f = open(target_file_name, "wb")
while True:
data = upload_file.file.read(8192)
if not data:
break
f.write(data)
f.close()
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top