如何在 python 中压缩 shutil.make_archive 的文件?
How to compress a file with shutil.make_archive in python?
我想使用 shutil.make_archive
命令压缩一个文本文件。我正在使用以下命令:
shutil.make_archive('gzipped'+fname, 'gztar', os.path.join(os.getcwd(), fname))
OSError: [Errno 20] Not a directory: '/home/user/file.txt'
我尝试了几种变体,但它一直试图压缩整个文件夹。如何正确地做到这一点?
试试这个并检查 shutil
将您的文件复制到一个目录。
cd目录
shutil.make_archive('gzipped', 'gztar', os.getcwd())
shutil
无法从一个文件创建存档。您可以使用 tarfile
,而不是:
tar = tarfile.open(fname + ".tar.gz", 'w:qz')
os.chdir('/home/user')
tar.add("file.txt")
tar.close()
或
tar = tarfile.open(fname + ".tar.gz", 'w:qz')
tar.addfile(tarfile.TarInfo("/home/user/file.txt"), "/home/user/file.txt")
tar.close()
其实shutil.make_archive
可以制作一个文件的存档!只需将目标目录的路径作为 root_dir
并将目标文件名作为 base_dir
.
试试这个:
import shutil
file_to_zip = 'test.txt' # file to zip
target_path = 'C:\test_yard\' # dir, where file is
try:
shutil.make_archive(target_path + 'archive', 'zip', target_path, file_to_zip)
except OSError:
pass
如果您不介意执行文件复制操作:
def single_file_to_archive(full_path, archive_name_no_ext):
tmp_dir = tempfile.mkdtemp()
shutil.copy2(full_path, tmp_dir)
shutil.make_archive(archive_name_no_ext, "zip", tmp_dir, '.')
shutil.rmtree(tmp_dir)
@CommonSense 有一个很好的答案,但文件将始终在其父目录中创建压缩文件。如果您需要创建一个没有额外目录的 zip 文件,只需直接使用 zipfile
模块
import os, zipfile
inpath = "test.txt"
outpath = "test.zip"
with zipfile.ZipFile(outpath, "w", compression=zipfile.ZIP_DEFLATED) as zf:
zf.write(inpath, os.path.basename(inpath))
将目录存档到另一个目的地对我来说是一个难题,但 帮助很大。
from shutil import make_archive
make_archive(
base_name=path_to_directory_to_archive},
format="gztar",
root_dir=destination_path,
base_dir=destination_path)
我想使用 shutil.make_archive
命令压缩一个文本文件。我正在使用以下命令:
shutil.make_archive('gzipped'+fname, 'gztar', os.path.join(os.getcwd(), fname))
OSError: [Errno 20] Not a directory: '/home/user/file.txt'
我尝试了几种变体,但它一直试图压缩整个文件夹。如何正确地做到这一点?
试试这个并检查 shutil
将您的文件复制到一个目录。
cd目录
shutil.make_archive('gzipped', 'gztar', os.getcwd())
shutil
无法从一个文件创建存档。您可以使用 tarfile
,而不是:
tar = tarfile.open(fname + ".tar.gz", 'w:qz')
os.chdir('/home/user')
tar.add("file.txt")
tar.close()
或
tar = tarfile.open(fname + ".tar.gz", 'w:qz')
tar.addfile(tarfile.TarInfo("/home/user/file.txt"), "/home/user/file.txt")
tar.close()
其实shutil.make_archive
可以制作一个文件的存档!只需将目标目录的路径作为 root_dir
并将目标文件名作为 base_dir
.
试试这个:
import shutil
file_to_zip = 'test.txt' # file to zip
target_path = 'C:\test_yard\' # dir, where file is
try:
shutil.make_archive(target_path + 'archive', 'zip', target_path, file_to_zip)
except OSError:
pass
如果您不介意执行文件复制操作:
def single_file_to_archive(full_path, archive_name_no_ext):
tmp_dir = tempfile.mkdtemp()
shutil.copy2(full_path, tmp_dir)
shutil.make_archive(archive_name_no_ext, "zip", tmp_dir, '.')
shutil.rmtree(tmp_dir)
@CommonSense 有一个很好的答案,但文件将始终在其父目录中创建压缩文件。如果您需要创建一个没有额外目录的 zip 文件,只需直接使用 zipfile
模块
import os, zipfile
inpath = "test.txt"
outpath = "test.zip"
with zipfile.ZipFile(outpath, "w", compression=zipfile.ZIP_DEFLATED) as zf:
zf.write(inpath, os.path.basename(inpath))
将目录存档到另一个目的地对我来说是一个难题,但
from shutil import make_archive
make_archive(
base_name=path_to_directory_to_archive},
format="gztar",
root_dir=destination_path,
base_dir=destination_path)