unzip zip files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DataSmash

    #1

    unzip zip files

    I need to unzip all zip file(s) in the current directory
    into their own subdirectories. The zip file name(s) always
    start with the string "usa" and end with ".zip".
    The code below will make the subdirectory, move the zip
    file into the subdirectory, but unzips the contents into the
    root (current) directory. I want the contents of the zip file
    unloaded into the newly created subdirectory where the zip file is.

    Any ideas?
    Thanks.
    R.D.

    import subprocess

    # Get all the zip files in the current directory.
    for zip in os.listdir(''):
    if zip.endswith(". zip"):

    # Remove the first 3 and the last 4 characters
    # e.g. usa12345.zip becomes 12345
    zipBase = zip[3:-4]

    # Make directory for unzipping
    os.mkdir(zipBas e)

    # Move the zip file to the subdirectory
    shutil.move(zip , zipBase)

    # Make system call "unzip"
    subprocess.Pope n(["unzip", zipBase + "\\" + zip]).wait()

  • Marcelo Ramos

    #2
    Re: unzip zip files

    DataSmash escribió:[color=blue]
    > I need to unzip all zip file(s) in the current directory
    > into their own subdirectories. The zip file name(s) always
    > start with the string "usa" and end with ".zip".
    > The code below will make the subdirectory, move the zip
    > file into the subdirectory, but unzips the contents into the
    > root (current) directory. I want the contents of the zip file
    > unloaded into the newly created subdirectory where the zip file is.
    >
    > Any ideas?
    > Thanks.
    > R.D.
    >
    > import subprocess
    >
    > # Get all the zip files in the current directory.
    > for zip in os.listdir(''):
    > if zip.endswith(". zip"):
    >
    > # Remove the first 3 and the last 4 characters
    > # e.g. usa12345.zip becomes 12345
    > zipBase = zip[3:-4]
    >
    > # Make directory for unzipping
    > os.mkdir(zipBas e)
    >
    > # Move the zip file to the subdirectory
    > shutil.move(zip , zipBase)
    >
    > # Make system call "unzip"
    > subprocess.Pope n(["unzip", zipBase + "\\" + zip]).wait()
    >
    >[/color]

    See "-d" zip's parameter in man zip.

    Regards.

    --
    Marcelo Ramos
    Fedora Core 5 | 2.6.16
    Socio UYLUG Nro 125

    Comment

    • Edward Elliott

      #3
      Re: unzip zip files

      Marcelo Ramos wrote:
      [color=blue]
      > DataSmash escribió:[color=green]
      >> file into the subdirectory, but unzips the contents into the
      >> root (current) directory. I want the contents of the zip file
      >> unloaded into the newly created subdirectory where the zip file is.
      >>[/color]
      >
      > See "-d" zip's parameter in man zip.[/color]

      or change the working dir before calling unzip.

      --
      Edward Elliott
      UC Berkeley School of Law (Boalt Hall)
      complangpython at eddeye dot net

      Comment

      • Jay Parlar

        #4
        Re: unzip zip files


        On May 12, 2006, at 5:45 PM, DataSmash wrote:
        [color=blue]
        > I need to unzip all zip file(s) in the current directory
        > into their own subdirectories. The zip file name(s) always
        > start with the string "usa" and end with ".zip".
        > The code below will make the subdirectory, move the zip
        > file into the subdirectory, but unzips the contents into the
        > root (current) directory. I want the contents of the zip file
        > unloaded into the newly created subdirectory where the zip file is.
        >
        > Any ideas?
        > Thanks.
        > R.D.
        >
        > import subprocess
        >
        > # Get all the zip files in the current directory.
        > for zip in os.listdir(''):
        > if zip.endswith(". zip"):
        >
        > # Remove the first 3 and the last 4 characters
        > # e.g. usa12345.zip becomes 12345
        > zipBase = zip[3:-4]
        >
        > # Make directory for unzipping
        > os.mkdir(zipBas e)
        >
        > # Move the zip file to the subdirectory
        > shutil.move(zip , zipBase)
        >
        > # Make system call "unzip"
        > subprocess.Pope n(["unzip", zipBase + "\\" + zip]).wait()
        >[/color]


        Ouch... Is there any reason that you're using subprocess+'unz ip', as
        opposed to using the 'zipfile' module from the standard library?

        Jay P.

        Comment

        • DataSmash

          #5
          Re: unzip zip files

          Thanks!
          I ended up using the "-d" parameter.
          I did try the zipfile module but I couldn't figure it out, nor could I
          find any examples using it.
          I also didn't have any luck changing the working dircectory and making
          it work.

          import subprocess, os

          # Get all the zip files in the current directory.
          for zip in os.listdir(''):
          if zip.endswith(". zip"):

          # Remove the first 3 and the last 4 characters
          # e.g. usa12345.zip becomes 12345
          zipBase = zip[3:-4]

          # Make directory for unzipping
          os.mkdir(zipBas e)

          # Make system call "unzip"
          print "\n unzip -d", zipBase, zip
          subprocess.Pope n(["unzip", "-d", zipBase, zip]).wait()

          Comment

          Working...