Python - 搜索文件和文件ZIP,跨多个目录

发布于 2024-10-04 11:18:28 字数 976 浏览 3 评论 0原文

这是我第一次将零碎的代码组合在一起形成我需要的实用程序(我是一名设计师),尽管我觉得我已经很接近了,但我在让以下内容正常工作时遇到了困难。

我通常需要压缩我创建的目录结构中带有 .COD 扩展名的文件。作为示例,结构可能如下所示:(

单个根文件夹)-> (多个文件夹)-> (两个文件夹)-> (一个文件夹)-> COD 文件

我需要将所有 COD 文件压缩到 COD.zip 中,并将该 zip 文件放置在当前文件所在目录的上一级目录中。完成后,文件夹结构将如下所示:

EXPORT 文件夹 -> 9800文件夹-> 6文件夹-> OTA 文件夹(+ 新 COD.zip)-> COD 文件

我的问题 -

首先,它创建的 COD.zip 似乎适合其中的 COD 文件,但当我解压它时,里面只有 1 个 .cod,但该 ZIP 的文件大小是所有压缩包的大小COD 压缩在一起。

其次,我需要将 COD 文件压缩,不带任何文件夹结构 - 直接在 COD.zip 中。目前,我的脚本创建了整个目录结构(以“users/mysuername/etc 等”开头)。

任何帮助将不胜感激 - 并且解释会更好,因为我正在努力学习:)

谢谢。

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())

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())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

臻嫒无言 2024-10-11 11:18:28

对于问题#1,我认为您的问题可能是这一部分:

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

每次写入新文件时,您都会创建一个新的 zip(并可能覆盖现有的)。相反,您希望为每个目录创建一次 zip,然后重复附加到该目录(请参见下面的示例)。

对于问题#2,您的问题是,当您将文件名写入存档时,您可能需要将文件名扁平化。一种方法是使用 os.chdir 将 CD 插入到 scandirs 中的每个目录中。一种更简单的方法是使用 os.path 模块分割文件路径并获取基本名称(不带路径的文件名),然后您可以使用 cod.write 的第二个参数 更改放入实际 zi​​p 中的文件名(请参见下面的示例)。

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())

因此,创建一次 zip 文件,在扁平化文件名的同时重复附加到该文件,然后将其关闭。您还需要确保调用 close,否则可能无法写入所有文件。

目前我没有一个好的方法在本地测试这个,所以请随意尝试并报告回来。我确信我可能弄坏了东西。 ;-)

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. ;-)

摘星┃星的人 2024-10-11 11:18:28

以下代码具有相同的效果,但可重用性更高,并且不会创建多个 zip 文件。

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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文