Python tarfile progress output?

前端 未结 6 988
旧巷少年郎
旧巷少年郎 2021-02-07 10:41

I\'m using the following code to extract a tar file:

import tarfile
tar = tarfile.open(\"sample.tar.gz\")
tar.extractall()
tar.close()

However,

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-07 10:56

    You can just use tqdm() and print the progress of the number of files being extracted:

    import tarfile
    from tqdm import tqdm
    
    # open your tar.gz file
    with tarfile.open(name=path) as tar:
    
        # Go over each member
        for member in tqdm(iterable=tar.getmembers(), total=len(tar.getmembers())):
    
            # Extract member
            tar.extract(member=member)
    

提交回复
热议问题