471,579 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,579 software developers and data experts.

uploading files to file system/zipping/downloading problems

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
3 1535
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
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
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.

Similar topics

15 posts views Thread by tony melnyk | last post: by
5 posts views Thread by Nathan Sokalski | last post: by
2 posts views Thread by Andy | last post: by
reply views Thread by najmi | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
1 post views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.