Python tarfile progress output?

前端 未结 6 1080
旧巷少年郎
旧巷少年郎 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条回答
  •  萌比男神i
    2021-02-07 10:56

    You can specify the members parameter in extractall()

    with tarfile.open(, 'r') as tarball:
       tarball.extractall(path=, members = track_progress(tarball))
    
    def track_progress(members):
       for member in members:
          # this will be the current file being extracted
          yield member
    

    member are TarInfo objects, see all available functions and properties here

提交回复
热议问题