Extract files from zip without keeping the structure using python ZipFile?

后端 未结 5 1655
栀梦
栀梦 2020-11-27 16:10

I try to extract all files from .zip containing subfolders in one folder. I want all the files from subfolders extract in only one folder without keeping the original struc

5条回答
  •  忘掉有多难
    2020-11-27 16:34

    In case you are getting badZipFile error. you can unzip the archive using 7zip sub process. assuming you have installed the 7zip then use the following code.

    import subprocess
    my_dir = destFolder #destination folder
    my_zip = destFolder + "/" + filename.zip #file you want to extract
    ziploc = "C:/Program Files/7-Zip/7z.exe" #location where 7zip is installed
    cmd = [ziploc, 'e',my_zip ,'-o'+ my_dir ,'*.txt' ,'-r' ] 
    #extracting only txt files and from all subdirectories
    sp = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
    

提交回复
热议问题