python tar file how to extract file into stream
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to extract a zipped folder but instead of directly using .extractall() , I want to extract the file into stream so that I can handle the stream myself. Is it possible to do it using tarfile ? Or is there any suggestions? 回答1: You can obtain each file from a tar file as a python file object using the .extractfile() method. Loop over the tarfile.TarFile() instance to list all entries: import tarfile with tarfile.open(path) as tf: for entry in tf: # list each entry one by one fileobj = tf.extractfile(entry) # fileobj is now an open