0

I am trying to call this command tar -zxvf file.tar.gz from a python script and I'm having trouble with it. I've read some relevant q/a's but I still haven't figured out how to do it.

I was thinking to fit my code to the original command:

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

So far I've made several attempts, as shown below

stdin = file.tar.gz
subprocess.check_call(-zxvf, stdin)

Any help? Thanks.

Ashoka Lella
  • 6,631
  • 1
  • 30
  • 39

1 Answers1

4

You can untar a file using the tarfile module

import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()
Ashoka Lella
  • 6,631
  • 1
  • 30
  • 39
  • what if instead of `all()` I need to extract something in particular?? –  Jul 17 '14 at 09:55
  • @EnKei, I'm not sure how to do that. you can probably have a look at the `tarfile` moudles documentation online – Ashoka Lella Jul 17 '14 at 09:56
  • 1
    @EnKei This might help for extracting a single file. The answer shows looping through the tar contents, you'll need to add code to match the file you want, but you can extract with tar.extractfile("file") http://stackoverflow.com/questions/13562037/python-tar-file-how-to-extract-file-into-stream [tarfile docs](https://docs.python.org/2/library/tarfile.html#examples) –  Jul 17 '14 at 10:10