title: Copy content of zipfile in Python
body: <p>I'm looking to recursively search through a network directory to identify all <code>.xls</code> files in the <code>zip</code> files. For each <code>XLS</code> file found in the <code>zip</code> file, I would like to copy it out to a local location in <code>C:</code>. Here is my script so far:</p>
  
  <pre><code>import os
  import zipfile
  import fnmatch
  import shutil
  
  rootPath = "L:\Data\Cases"
  destPath = "C:\Test"
  allFileList = []
  zipList = []
  
  # Create a list containing all files contained within L:\Data\Cases
  for dirname, dirnames, filenames in os.walk(rootPath):
      for filename in filenames:
          allFileList.append(os.path.join(dirname, filename))
  
  # Return a list of filepaths containing zipfiles.
  for file in allFileList:
      if file.endswith(".zip"):
          zipList.append(file)
  
  for file in zipList:
      with zipfile.ZipFile(file) as zip_file:
          for member in zip_file.namelist():
              if member.endswith(".xls"):
                  filename = os.path.basename(member)
                  if not filename:
                      continue
                  source = zip_file.open(member)
                  target = os.path.join(destPath, filename)
                  shutil.copy2(source, target)
  </code></pre>
  
  <p>The error code is below. I think the error is being caused by copying what is in effect a file in a compressed container to the target path.</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:/Users/user/Desktop/parsecsv.py", line 30, in &lt;module&gt;
      shutil.copy2(source, target)
    File "C:\Program Files\Python278\lib\shutil.py", line 130, in copy2
      copyfile(src, dst)
    File "C:\Program Files\Python278\lib\shutil.py", line 68, in copyfile
      if _samefile(src, dst):
    File "C:\Program Files\Python278\lib\shutil.py", line 63, in _samefile
      return (os.path.normcase(os.path.abspath(src)) ==
    File "C:\Program Files\Python278\lib\ntpath.py", line 487, in abspath
      path = _getfullpathname(path)
  </code></pre>
  
  <p>Any suggestions?</p>