I want to extract multiple zip folders for each folder.

Asked 2 years ago, Updated 2 years ago, 33 views

"I extracted several zip files at once using the function below, but all the contents were expanded to the same folder at the top called ""XBRLData""."
Actually, I would like it to be expanded by each zip folder, so please let me know where to change it.

def open_zip():

    os.chdir(r"C:\Users\osamu\Desktop\VS_Code\TDNet\yanoshin_API\original\data_original\\")
    zip_fs=glob.glob('*.zip') 
    
    for fin zip_fs:
        with zipfile.ZipFile(f) as zip_f:
            zip_f.extractall()

Enter a description of the image here

python python3

2022-09-30 13:56

1 Answers

With reference to this article and specifications documentation, I do not specify anything when unzipping (zip_f.extractall()), but why don't you specify a folder name to unzipping to?
With Python's 74th Python, starting from scratch, you can extract 100 files to spare

The following is an example of extracting a ZIP file called "test.zip" and expanding it below the folder called "test".

import zipfile
with zipfile.ZipFile('test.zip', 'r') asf:
    f.extractall('./test')#### Fix the original article as it appears indented

After saving the above program as test-unzip.py, launch IDLE in Python's running environment.Try loading and running the program from the IDLE menu File>Open. If successful, the contents of the ZIP file will be extracted below the folder called test.

ZipFile.extractall(path=None, members=None, pwd=None)

Expand all members from the archive to the current working directory.path specifies the directory to deploy to.

This article is not extractall() but extract(), but it is the same.
How to unzip specific folder from a.zip with Python

Or, if you want to keep the hierarchy as it is now, why don't you rename the XBRLData folder with os.rename() immediately after unzipping?
Change the filename or directory name
os.rename(src,dst,*,src_dir_fd=None,dst_dir_fd=None)

In any case, it seems that the target folder name is either an extension removed from the ZipFile file name, or something has been done based on it.
PurePath.stem

The path element ends with no extensions:


2022-09-30 13:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.