Python 尝试仅在一个目录中压缩文件

Python 尝试仅在一个目录中压缩文件,python,zip,Python,Zip,我有包含文件和子目录的路径/home/mine/new。我只想压缩该路径中的文件(在“新建”路径中),以便我的压缩文件夹包含/new/file1、新建/file2等 myzip.write(os.path.join(path, file)) 我试过这个: import zipfile import os,glob def zipfunc(path, myzip): for path,dirs, files in os.walk(path): for file

我有包含文件和子目录的路径/home/mine/new。我只想压缩该路径中的文件(在“新建”路径中),以便我的压缩文件夹包含
/new/file1
新建/file2

myzip.write(os.path.join(path, file))
我试过这个:

import zipfile
import os,glob


def zipfunc(path, myzip):
    for path,dirs, files in os.walk(path):
            for file in files:
                if  os.path.isfile(os.path.join(path,file)):
                    myzip.write(os.path.join(os.path.basename(path), file))


if __name__ == '__main__':
    path=r'/home/ggous/new'
    myzip = zipfile.ZipFile('myzipped.zip', 'w')
    zipfunc(path,myzip)
    myzip.close()
myzip.write(os.path.join(path, file))
但它给了我一个错误

没有这样的文件或目录new/file.doc

,这是因为
os.walk
将下降到子目录中(并且您的路径连接逻辑无法正确处理该问题)。如果不想递归到子目录中,请使用
glob.glob
而不是
os.walk

from os import path
import os

directory = r'/home/ggous/new'
for f in os.listdir(directory):
     if path.isfile(f):
         zipfile.write(os.path.basename(f), f)
myzip.write(os.path.join(path, file))

您正在对path变量调用
os.path.basename
。这意味着,如果您试图压缩文件
new/dir1/file1
,您将删除路径的
new/
部分,最后得到的是
dir1/file1
,它不是相对于当前目录的有效路径

myzip.write(os.path.join(path, file))
如果您只需删除对
os.path.basename
的调用,它将正确压缩文件:

myzip.write(os.path.join(path, file))
…虽然就路径而言,这可能不是zipfile中所需的。如果从顶级目录中启动
os.walk
,您可能会得到您想要的:

myzip.write(os.path.join(path, file))
if __name__ == '__main__':
    path=os.path.join(os.getcwd(), 'new')
    myzip = zipfile.ZipFile('myzipped.zip', 'w')

    # Change into the top level directory.
    os.chdir(path)

    zipfunc('.',myzip)
    myzip.close()
考虑到这样的层次结构:

myzip.write(os.path.join(path, file))
/home/lars/new
  file1
  file2
  dir1/
    file1
    file2
    file3
Archive:  myzipped.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       29  11-02-2011 09:14   file1
       29  11-02-2011 09:14   file2
       29  11-02-2011 09:14   dir1/file3
       29  11-02-2011 09:14   dir1/file1
       29  11-02-2011 09:14   dir1/file2
---------                     -------
      145                     5 files
这将创建一个zipfile,如下所示:

myzip.write(os.path.join(path, file))
/home/lars/new
  file1
  file2
  dir1/
    file1
    file2
    file3
Archive:  myzipped.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       29  11-02-2011 09:14   file1
       29  11-02-2011 09:14   file2
       29  11-02-2011 09:14   dir1/file3
       29  11-02-2011 09:14   dir1/file1
       29  11-02-2011 09:14   dir1/file2
---------                     -------
      145                     5 files

如果您的实际目标是只在顶层压缩文件,而不向下延伸到任何子目录,那么您就不需要
os.walk
。您可以只使用
os.listdir
os.path.isfile
来确保您处理的是文件而不是子目录。

看起来可能是
zipfile.zipfile.write()
需要绝对路径的问题,而不是相对或不完整的路径。在压缩指向的文件之前,请尝试确保路径是绝对路径:

myzip.write(os.path.join(path, file))
def zipfunc(path, myzip):
    for path,dirs, files in os.walk(path):
            for file in files:
                curpath = os.path.abspath(os.path.join(path, file))
                if  os.path.isfile(curpath):
                    myzip.write(curpath)

:它工作得很好,但正如您上面所说,我不希望子文件夹和其他文件处于“新建”状态。我正在使用isfile方法,但结果仍然相同。好的,我使用了os.listdir,现在一切正常了!。如果我想在zipfile中不仅显示文件,还显示文件夹。例如:/new/file1、/new/file2,我如何添加它?因为我正在尝试basename,但它不起作用。如果你想包含父路径,那么不要将
os.chdir()
放入目录,而是继续使用
os.listdir()
os.path.join()
:@larsks:我试过了,但它创建了一个zipfile,其中包含相同的zipfile,当我尝试打开它时,它会给出“错误canot open file as archive”,上面会显示所有子目录及其文件。我只想要“new”文件夹中的文件。您好,这给了我一个错误“glob中f的行中的module object is not callable.”@George:那么您没有正确复制
import glob
行。您也可以使用
os.listdir
,顺便说一句@George:但我想不能使用glob import glob的