Results 1 to 8 of 8

Thread: zipfile in python

  1. #1
    Join Date
    Jul 2010
    Beans
    37

    zipfile in python

    I make a temp directory inside the current working directory, download images to temp, package them using zipfile, and then copy the zipfile out to another location. It works file, but the zipfile copies the folder structure of the temp folder, i.e. instead of an archive with 001.jpg, 002.jpg, 003.jpg...I have instead temp/001.jpg, temp/002.jpg, temp/003.jpg

    Any way to remedy this?

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: zipfile in python

    What's your code?
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Jul 2010
    Beans
    37

    Re: zipfile in python

    Code:
    def compress(mangaChapterPrefix, current_chapter, download_path, current_page, download_format):
    	print('Compressing...')
    	z = zipfile.ZipFile('mangadl_tmp/' + mangaChapterPrefix + download_format, 'a')
    	for page in range(1, current_page + 1):
    		z.write('mangadl_tmp/' + mangaChapterPrefix + '_' + str(page).zfill(3) + '.jpg')
    	shutil.move('mangadl_tmp/' + mangaChapterPrefix + download_format, download_path)
    	cleanTmp()
    The images are stored in mangadl_tmp. My intention is to create the zip archive inside the tmp folder as well, then move it out to download_path.

  4. #4
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: zipfile in python

    We're not supposed to help with illegal stuff, but have a look at the arcname argument to write().
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    Feb 2008
    Beans
    17

    Re: zipfile in python

    Here is something that might work:

    You could change to the "mangadl_tmp" directory before issuing the zip command:
    http://docs.python.org/library/os.html
    Last edited by Jacobian; August 3rd, 2010 at 07:01 PM.

  6. #6
    Join Date
    Jul 2010
    Beans
    37

    Re: zipfile in python

    Changing arcname fixed it for me. I assumed the default behavior of write was to grab the filename and not the full path passed to it, but this was not the case.

    Code:
    z.write('mangadl_tmp/' + mangaChapterPrefix + '_' + str(page).zfill(3) + '.jpg', mangaChapterPrefix + '_' + str(page).zfill(3) + '.jpg')

  7. #7
    Join Date
    Mar 2005
    Location
    Haarlem, The Netherlands
    Beans
    363

    Re: zipfile in python

    to clean up your code and to make it more portable across platforms I would introduce a variable for the filename, and I would use the join method in the os.path library, you get then
    Code:
    filename = mangaChapterPrefix + '_' + str(page).zfill(3) + '.jpg'
    z.write((os.path.join('mangadl_tmp', filename), filename))

  8. #8
    Join Date
    Jul 2010
    Beans
    37

    Re: zipfile in python

    Done - thanks for pointing this out. I'd actually been wondering about this for a long time.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •