python从url下载并打开tar.gz文件

  • archive file

    An archive file is a file that is composed of one or more computer files along with metadata.

    Archive files are used to collect multiple data files together into a single file for easier portability and storage, or simply to compress files to use less storage space.

  • tar

    GNU tar: an archiver tool

    In computing, tar is a computer software utility for collecting many files into one archive file, often referred to as a tarball, fordistribution or backup purposes.

  • gzip

    www.gzip.org

    gzip is a file format and a software application used for file compression and decompression.

    gzip is based on the DEFLATE algorithm, which is a combuination of LZ&& and Huffman coding.

  • tar.gz

    What is a tar.gz File, and How Do I Open It?

    Tar files are often compressed after being created, giving it the .tar.gz file extension.

    Technically these are TGZ files, but nearly everyone calls both .tar and .tar.gz files simple “tar files”.

  • open a tar file
    tar -xzf tarfile # extract
    tar -czf tar.tar.gz tar # create
    

    -v enables verbose mode, showing the progress of the commna

    -x extract

    -c create

    -z uses gzip, omit this if you hust have a .tar

    -f specifies file input, rather than STDIN

  • through python

    tarfile - Read and write tar archive files

    The tarfile module makes it possible to read and write tar archives, including those using gzip, bz2 and lzma compression.

    tarfile.ooen(name=None, mode='r', fileobj=None, bufsize=10240,**kwargs) return a TarFile object for the pathname name.

    TarFile = tarfile.open('path/file.tar.gz','r:gz')
    tf.list(verbose=False) # 查看文件内容
    tf.getnames() # 返回list,包内文件名
    tf.getmember(name) # 返回member
    tf.extract(member) # 提取文件
    tf.extractall() # 提取全部文件
    
  • 下载tar.gz文件

    import requests
    r = request.get(url)
    with open(filepath, 'wb') as f:
        f.write(r.content)
    
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值