Python 我可以选择在shutil中归档哪个文件吗?
我只想归档目录中的一些文件。假设以下目录结构:Python 我可以选择在shutil中归档哪个文件吗?,python,zip,shutil,Python,Zip,Shutil,我只想归档目录中的一些文件。假设以下目录结构: current_dir - current_file audio/ - fileA - fileB 在这种情况下,我是否可以从Python脚本(current_file)中仅存档audio/和fileA 我使用shutil,但是下面的命令似乎使用了特定目录中的所有文件 shutil.make_archive("new_file", 'zip', dir_name) 以下是实现您想要的目标的可能方法: import os im
current_dir
- current_file
audio/
- fileA
- fileB
在这种情况下,我是否可以从Python脚本(current_file
)中仅存档audio/
和fileA
我使用shutil
,但是下面的命令似乎使用了特定目录中的所有文件
shutil.make_archive("new_file", 'zip', dir_name)
以下是实现您想要的目标的可能方法:
import os
import zipfile
import glob
content = [
"audio",
"fileA"
]
def zip_dir(path, zip_file):
for root, dirs, files in os.walk(path):
for file in files:
zip_file.write(os.path.join(root, file))
zip_file = zipfile.ZipFile("mcve.zip", 'w')
for path in content:
if os.path.isdir(path):
zip_dir(path, zip_file)
else:
zip_file.write(path)
zip_file.close()
考虑使用<代码> ZLIB < /代码>代替<代码> SUTUL 。