"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()
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:
551 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
544 Unable to install versioned in Google Colab
551 PHP ssh2_scp_send fails to send files as intended
542 Uncaught (inpromise) Error on Electron: An object could not be cloned
736 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.