uploading files to file system/zipping/downloading problems

Discussion in 'Python' started by OriginalBrownster, Aug 20, 2006.

  1. 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??
     
    OriginalBrownster, Aug 20, 2006
    #1
    1. Advertisements

  2. You are re-creating, and overwriting, your file every time you go into
    that while loop.
     
    Pontus Ekberg, Aug 20, 2006
    #2
    1. Advertisements

  3. OriginalBrownster

    Jordan Guest

    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()
     
    Jordan, Aug 21, 2006
    #3
  4. OriginalBrownster

    Larry Bates Guest

    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
     
    Larry Bates, Aug 21, 2006
    #4
  5. 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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Aug 21, 2006
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.