0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

tarfileの圧縮・展開

Posted at
1
import tarfile

with tarfile.open('test.tar.gz', 'w:gz') as tr:
    tr.add('test_dir')
#test_dirというディレクトリをtest.tar.gzというファイル名でtarfileに圧縮

with tarfile.open('test.tar.gz', 'r:gz') as tr:
    tr.extractall(path='test_tar')
#test_tarというディレクトリにtest.tar.gzを展開
2
import tarfile

with tarfile.open('test.tar.gz', 'r:gz') as tr:
    with tr.extractfile('test_dir/sub_dir/sub_test.txt') as f:
        print(f.read())
#tarfileに圧縮されたtest.tar.gzを展開することなく、sub_test.txtを出力
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?