python压缩文件不带根路径_Python zip文件夹,不包括’./'(当前目录)

我正在尝试压缩测试文件夹的内容:

first.txt

pof/

pof/second.txt

如果我进入测试然后使用拉链

zip -r folder.zip *

并检查生成的存档

zipinfo folder.zip

我得到这个输出:

Archive: folder.zip

Zip file size: 7573 bytes, number of entries: 3

-rw-r--r-- 3.0 unx 6473 tx defN 16-Mar-11 10:19 first.txt

drwxr-xr-x 3.0 unx 0 bx stor 16-Mar-11 10:20 pof/

-rw-r--r-- 3.0 unx 2841 tx defN 16-Mar-11 10:20 pof/second.txt

3 files, 9314 bytes uncompressed, 7113 bytes compressed: 23.6%

一切似乎按预期工作,但如果我使用相同的文件夹拉链

shutil.make_archive('folder', 'zip', 'test')

然后检查存档

zipinfo folder.zip

我得到这个输出:

Archive: folder.zip

Zip file size: 7497 bytes, number of entries: 4

drwxr-xr-x 2.0 unx 0 b- defN 16-Mar-11 10:28 ./

drwxr-xr-x 2.0 unx 0 b- defN 16-Mar-11 10:20 pof/

-rw-r--r-- 2.0 unx 6473 b- defN 16-Mar-11 10:19 first.txt

-rw-r--r-- 2.0 unx 2841 b- defN 16-Mar-11 10:20 pof/second.txt

4 files, 9314 bytes uncompressed, 7113 bytes compressed: 23.6%

我不喜欢的是./包含在Python生成的zip存档中:我该如何避免这种情况?

解决方法:

确保测试位于其他空文件夹中并压缩该父文件夹. shutil.make_archive()包含您指定的文件夹中的所有内容,而不是文件夹本身:

$tree parent/

parent/

└── test

├── first.txt

└── pof

└── second.txt

$bin/python

Python 2.7.11 (default, Feb 20 2016, 23:04:20)

[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import shutil

>>> shutil.make_archive('folder', 'zip', 'parent')

'/.../folder.zip'

>>> ^Z

$zipinfo folder.zip

Archive: folder.zip 504 bytes 5 files

drwxr-xr-x 2.0 unx 0 b- stor 11-Mar-16 11:19 ./

drwxr-xr-x 2.0 unx 0 b- stor 11-Mar-16 11:19 test/

drwxr-xr-x 2.0 unx 0 b- stor 11-Mar-16 11:19 test/pof/

-rw-r--r-- 2.0 unx 0 b- defN 11-Mar-16 11:19 test/first.txt

-rw-r--r-- 2.0 unx 0 b- defN 11-Mar-16 11:19 test/pof/second.txt

5 files, 0 bytes uncompressed, 4 bytes compressed: 0.0%

但是,您无法阻止shutil.make_archive()包含./ current目录.在这种情况下编写自己的目录:

import os

import os.path

import zipfile

zip_filename = 'folder.zip'

base_dir = os.path.abspath('parent')

with zipfile.ZipFile(zip_filename, "w",

compression=zipfile.ZIP_DEFLATED) as zf:

base_path = os.path.normpath(base_dir)

for dirpath, dirnames, filenames in os.walk(base_dir):

for name in sorted(dirnames):

path = os.path.normpath(os.path.join(dirpath, name))

zf.write(path, os.path.relpath(path, base_path))

for name in filenames:

path = os.path.normpath(os.path.join(dirpath, name))

if os.path.isfile(path):

zf.write(path, os.path.relpath(path, base_path))

标签:python,zip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用Python的os和shutil模块来完成这个任务。以下是一个可能的解决方案: ```python import os import shutil # 创建新文件夹 if not os.path.exists('python_code'): os.mkdir('python_code') # 查找所有.py文件并复制到新文件夹 for filename in os.listdir('.'): if filename.endswith('.py'): shutil.copy(filename, 'python_code') # 压缩文件夹 shutil.make_archive('python_code', 'zip', 'python_code') ``` 这段代码首先创建了一个名为python_code的新文件夹,然后使用os.listdir()函数查找当前工作目录下的所有文件,如果文件名以.py结尾,则使用shutil.copy()函数将其复制到python_code文件夹中。最后,使用shutil.make_archive()函数将python_code文件夹压缩python_code.zip文件。 ### 回答2: 要实现这个功能,可以使用Python的os和shutil库。 首先,使用os模块的listdir函数来获取工作目录下的所有文件文件夹。使用os.path模块的isfile函数来判断一个路径是否为文件。 接下来,遍历工作目录下的所有文件文件夹,找出所有的Python文件。可以使用字符串的endswith方法来判断文件的后缀名是否为".py"。 然后,使用shutil库的copy函数将所有的Python文件复制到新建文件夹python_code下。 最后,使用shutil库的make_archive函数来压缩python_code文件夹,并指定压缩文件的名称为python_code.zip。 下面是完整的代码: ```python import os import shutil # 创建新建文件夹 os.makedirs("python_code", exist_ok=True) # 获取工作目录下的所有文件文件夹 files = os.listdir() # 遍历所有文件文件夹 for f in files: # 判断是否为文件 if os.path.isfile(f): # 判断是否为Python文件 if f.endswith(".py"): # 复制文件python_code文件夹 shutil.copy(f, "python_code") # 压缩python_code文件夹python_code.zip shutil.make_archive("python_code", "zip", root_dir="python_code") print("完成") ``` 运行以上代码后,工作目录下的所有Python文件将被复制到新建的python_code文件夹中,并且python_code文件夹将被压缩python_code.zip文件。 ### 回答3: 要实现这个目标, 首先需要使用python中的os和shutil模块进行文件操作,并且需要使用zipfile模块来压缩文件夹。下面是一个满足这个要求的代码示例: ```python import os import shutil import zipfile # 获取当前工作目录 current_dir = os.getcwd() # 新建文件夹python_code new_folder = os.path.join(current_dir, "python_code") os.makedirs(new_folder, exist_ok=True) # 查找工作目录下所有Python文件 python_files = [f for f in os.listdir(current_dir) if f.endswith(".py")] # 复制文件到新建文件夹python_code下 for file in python_files: source_path = os.path.join(current_dir, file) target_path = os.path.join(new_folder, file) shutil.copyfile(source_path, target_path) # 压缩文件夹python_code为python_code.zip zip_name = os.path.join(current_dir, "python_code.zip") with zipfile.ZipFile(zip_name, "w") as zipf: for foldername, subfolders, filenames in os.walk(new_folder): for filename in filenames: file_path = os.path.join(foldername, filename) arcname = os.path.relpath(file_path, new_folder) zipf.write(file_path, arcname) print("操作完成!") ``` 使用这段代码,它会查找当前工作目录下所有的Python文件,并将它们复制到一个新建的文件夹"python_code"中。然后,它将压缩整个文件夹为"python_code.zip"。请确保你有权限进行这些文件操作,否则可能会出现权限错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值