Zipping files

Discussion in 'Python' started by dp_pearce, Jul 15, 2008.

  1. dp_pearce

    dp_pearce Guest

    Hi all,

    I have come across an error while using zipfile and I can't seem to
    find somewhere that explains the problem. My script needs to be able
    to take text files from one drive and add them to zip files on another
    drive. The following seems to work just fine.

    import zipfile

    # write test file in working directory directory
    folder = "J:/"
    filename = "testing.txt"
    fullpath = folder+filename
    fout = open(fullpath, 'wt')
    fout.write(str1)
    fout.close()

    # Add text file to zip file on same drive
    zFolder = "J:/"
    zFilename = "testing.zip"
    zFullpath = zFolder+zFilename
    zout = zipfile.ZipFile(zFullpath, "w")
    zout.write(fullpath)
    zout.close()
    print fullpath, "successfully added to", zFullpath

    However, if I change the drive letters to anything other than the
    drive from which the Python script is saved (e.g. run script from J:
    but zip to C:), I get the following warning:

    Traceback (most recent call last):
    File "J:/test.py", line 18, in <module>
    zout = zipfile.ZipFile(zFullpath, "w")
    File "C:\Python25\lib\zipfile.py", line 339, in __init__
    self.fp = open(file, modeDict[mode])
    IOError: [Errno 13] Permission denied: 'C:/testing.zip'

    Can anyopne shed some light on what I am missing here? If it has any
    relevance to the permissions part of the error, I am currently using
    Windows machines.

    Thanks in advance for your time.

    Dan
     
    dp_pearce, Jul 15, 2008
    #1
    1. Advertising

  2. dp_pearce

    Tim Golden Guest

    dp_pearce wrote:
    > Hi all,
    >
    > I have come across an error while using zipfile and I can't seem to
    > find somewhere that explains the problem. My script needs to be able
    > to take text files from one drive and add them to zip files on another
    > drive. The following seems to work just fine.
    >
    > import zipfile
    >
    > # write test file in working directory directory
    > folder = "J:/"
    > filename = "testing.txt"
    > fullpath = folder+filename
    > fout = open(fullpath, 'wt')
    > fout.write(str1)
    > fout.close()
    >
    > # Add text file to zip file on same drive
    > zFolder = "J:/"
    > zFilename = "testing.zip"
    > zFullpath = zFolder+zFilename
    > zout = zipfile.ZipFile(zFullpath, "w")
    > zout.write(fullpath)
    > zout.close()
    > print fullpath, "successfully added to", zFullpath
    >
    > However, if I change the drive letters to anything other than the
    > drive from which the Python script is saved (e.g. run script from J:
    > but zip to C:), I get the following warning:
    >
    > Traceback (most recent call last):
    > File "J:/test.py", line 18, in <module>
    > zout = zipfile.ZipFile(zFullpath, "w")
    > File "C:\Python25\lib\zipfile.py", line 339, in __init__
    > self.fp = open(file, modeDict[mode])
    > IOError: [Errno 13] Permission denied: 'C:/testing.zip'
    >
    > Can anyopne shed some light on what I am missing here? If it has any
    > relevance to the permissions part of the error, I am currently using
    > Windows machines.



    Is it possible that you don't in fact have permission
    to write to c:\? I seem to recall that non-power-users
    on XP don't have that permission, and I bet that Vista
    makes that even more restrictive.

    TJG
     
    Tim Golden, Jul 15, 2008
    #2
    1. Advertising

  3. dp_pearce

    dp_pearce Guest

    When I saw "Permission denied", this was my suspicion. And I think you
    are very right. I have just gone back and tried writing to a file
    outside of C:, in this case C:/output/, and it seems to work again.

    Would I be right in guessing there is no way around this?

    Dan
     
    dp_pearce, Jul 15, 2008
    #3
  4. dp_pearce

    Tim Golden Guest

    dp_pearce wrote:
    > When I saw "Permission denied", this was my suspicion. And I think you
    > are very right. I have just gone back and tried writing to a file
    > outside of C:, in this case C:/output/, and it seems to work again.
    >
    > Would I be right in guessing there is no way around this?


    Well, you could (presumably) alter your own user's
    groups / rights and/or the permissions on the root
    of the C: drive so that you *can* zip to that location.

    On the other hand, chucking random stuff into the
    root of the system drive has never been considered
    a brilliant idea. (Altho' I know a lot of people
    who do it :) ). Why not create a suitable folder
    lower down, or in your own %APPDATA% area, or using
    the tempfile module, depending on your needs?

    TJG
     
    Tim Golden, Jul 15, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug Tolton

    Zipping and Unzipping files

    Doug Tolton, Nov 21, 2003, in forum: Python
    Replies:
    1
    Views:
    320
    Peter Hansen
    Nov 21, 2003
  2. OriginalBrownster

    Zipping Files to user defined Directory

    OriginalBrownster, Jul 28, 2006, in forum: Python
    Replies:
    4
    Views:
    336
    Danny Milosavljevic
    Sep 2, 2006
  3. OriginalBrownster

    Zipping files/zipfile module

    OriginalBrownster, Aug 2, 2006, in forum: Python
    Replies:
    5
    Views:
    469
    Brian Beck
    Aug 2, 2006
  4. OriginalBrownster
    Replies:
    4
    Views:
    339
    Dennis Lee Bieber
    Aug 21, 2006
  5. Arthur
    Replies:
    2
    Views:
    127
    Arthur
    Nov 30, 2003
Loading...

Share This Page