开发者

Python - Search for files & ZIP, across multiple directories

This is my first time hacking together bits and pieces of code to form a utility that I need (I'm a designer by trade) and, though I feel I'm close, I'm having trouble getting the following to work.

I routinely need to zip up files with a .COD extension that 开发者_开发知识库are inside of a directory structure I've created. As an example, the structure may look like this:

(single root folder) -> (multiple folders) -> (two folders) -> (one folder) -> COD files

I need to ZIP up all the COD files into COD.zip and place that zip file one directory above where the files currently are. Folder structure would look like this when done for example:

EXPORT folder -> 9800 folder -> 6 folder -> OTA folder (+ new COD.zip) -> COD files

My issues -

first, the COD.zip that it creates seems to be appropriate for the COD files within it but when I unzip it, there is only 1 .cod inside but the file size of that ZIP is the size of all the CODs zipped together.

second, I need the COD files to be zipped w/o any folder structure - just directly within COD.zip. Currently, my script creates an entire directory structure (starting with "users/mysuername/etc etc").

Any help would be greatly appreciated - and explanations even better as I'm trying to learn :)

Thanks.

import os, glob, fnmatch, zipfile


def scandirs(path):
for currentFile in glob.glob( os.path.join(path, '*') ):
    if os.path.isdir(currentFile):
        scandirs(currentFile)
    if fnmatch.fnmatch(currentFile, '*.cod'):
            cod = zipfile.ZipFile("COD.zip","a")
            cod.write(currentFile)


scandirs(os.getcwd())


For problem #1, I think your problem is probably this section:

cod = zipfile.ZipFile("COD.zip","a")
cod.write(currentFile)

You're creating a new zip (and possibly overwriting the existing one) every time you go to write a new file. Instead you want to create the zip once per directory and then repeatedly append to it (see example below).

For problem #2, your issue is that you probably need to flatten the filename when you write it to the archive. One approach would be to use os.chdir to CD into each directory in scandirs as you look at it. An easier approach is to use the os.path module to split up the file path and grab the basename (the filename without the path) and then you can use the 2nd parameter to cod.write to change the filename that gets put into the actual zip (see example below).

import os, os.path, glob, fnmatch, zipfile

def scandirs(path):

   #zip file goes at current path, then up one dir, then COD.zip
   zip_file_path = os.path.join(path,os.path.pardir,"COD.zip")
   cod = zipfile.ZipFile(zip_file_path,"a") #NOTE: will result in some empty zips at the moment for dirs that contain no .cod files

   for currentFile in glob.glob( os.path.join(path, '*') ):
      if os.path.isdir(currentFile):
         scandirs(currentFile)
      if fnmatch.fnmatch(currentFile, '*.cod'):
         cod.write(currentFile,os.path.basename(currentFile))

   cod.close()
   if not cod.namelist(): #zip is empty
      os.remove(zip_file_path)

scandirs(os.getcwd())

So create the zip file once, repeatedly append to it while flattening the filenames, then close it. You also need to make sure you call close or you may not get all your files written.

I don't have a good way to test this locally at the moment, so feel free to try it and report back. I'm sure I probably broke something. ;-)


The following code has the same effect but is more reusable and does not create multiple zip files.

import os,glob,zipfile

def scandirs(path, pattern):
    result = []
    for file in glob.glob( os.path.join( path, pattern)):
        if os.path.isdir(file):
            result.extend(scandirs(file, pattern))
        else:
             result.append(file)
     return result


zfile = zipfile.ZipFile('yourfile.zip','w')
for file in scandirs(yourbasepath,'*.COD'):
    print 'Processing file: ' + file
    zfile.write(file)                   # folder structure
    zfile.write(file, os.path.split(file)[1])   # no folder structure

zfile.close()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜