for ex abc.tar.gz has
abc/file1.txt
abc/file2.txt
abc/abc1/file3.txt
abc/abc2/file4.txt
i need to read/display the contents of file3.txt without extracting the file.
Thanks for any input.
Use -t switch with tar command to list content of a archive. tar file without actually extracting. You can see that output is pretty similar to the result of ls -l command.
If you need to check the contents of a compressed text file on Linux, you don't have to uncompress it first. Instead, you can use a zcat or bzcat command to extract and display file contents while leaving the file intact. The "cat" in each command name tells you that the command's purpose is to display content.
import tarfile
spam = tarfile.open( "abc.tar.gz" )
if "abc/abc1/file3.txt" in spam.getnames():
with spam.extractfile( "abc/abc1/file3.txt" ) as ham:
print ham.read()
See tarfile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With