'How to extract multiple 7z file using Python?

I would like to extract multiple .7z files using Python. I've tried this, but it only extracted one file. I already put in a loop.

Below is what I've tried.

import os.path
import glob
from pyunpack import Archive

os.chdir("E:/DATA/raw")
for file in glob.glob("*myfile.7z"):
    print(file)
    Archive(file).extractall("E:/DATA/output")

The names of the 7z files are:

AHFWHSH_1438923_myfile.7z
KFWFAUF_3257485_myfile.7z
GDSHUHG_8975498_myfile.7z

My expected output folders are:

output1
output2
output3


Solution 1:[1]

If your expected output is output1, output2, output3, then you should add the index to the output path. You should also create the directories before extracting the files, by using os.mkdir():

import os
import glob
from pyunpack import Archive

for i, f in enumerate(glob.glob(os.path.join("E:/DATA/raw", "*myfile.7z"))):
    dir_path = "E:/DATA/output" + str(i+1)
    os.mkdir(dir_path)
    Archive(f).extractall(dir_path)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1