0

i want to extract all files from multiple tar-archives, but whatever i do, it won't work. Lets say, i have two tar files in C:\Temp. So i thought, all i have to do is to get a list of the *.tar files in C:\temp, and extract it with a loop:

import glob
import tarfile

archivelist = glob.glob('C:\\Temp\\*.tar')
for x in archivelist:
    tar=tarfile.open(archivelist)
    tar.extractall()
    tar.close()

What is the problem? Even after hours of searching, i couldn't find a helpful example...

Thanks!

Fabster
  • 133
  • 1
  • 5
  • 5
    Just a wild guess: `tar=tarfile.open(x)`. It looks like you are passing the entire list instead of a file name. – hughdbrown Feb 07 '14 at 17:13
  • Thanks a lot, it can be so simple sometimes :) – Fabster Feb 07 '14 at 17:35
  • Further: (1) I would build the path using `os.path`: `import os.path; p = os.path.join(['C:', Temp', '*.tar'])` -- caveat: I don't use Windows. (2) I would place the `glob` call in the scope of the loop: `for x in glob.glob(p)` -- then you would not have had an `archivelist` variable to misplace. – hughdbrown Feb 07 '14 at 17:49
  • 1
    You might also consider using `tempfile.gettempdir()` instead of using magic strings. – Hyperboreus Feb 07 '14 at 19:20

0 Answers0