[英]Extract all files from a zipped folder inside a directory to other directory without folder using python
# importing required modules
from zipfile import ZipFile
# specifying the zip file name
file_name = "C:\\OPR\\109521P.zip"
# opening the zip file in READ mode
with ZipFile(file_name, 'r') as zip:
# printing all the contents of the zip file
result =zip.printdir()
# extracting all the files
print('Extracting all the files now...')
zip.extractall('images')
print('Done!')
我有大約 10 張圖像壓縮在一個壓縮文件夾的子文件夾中,現在我想將所有圖像直接解壓縮到其他目錄而不使用子文件夾,我嘗試使用 os.path.basename(name) ,但我得到了嚴重錯誤。
在上面的代碼之后,我將所有圖像放入一個文件夾中,,
C:\\圖像\\109521P
以上是提取所有10張圖像的輸出位置,現在我想直接提取圖像
C:\\圖像
所以我想省略子文件夾 109521P 並希望在上面的位置直接提取圖像。
my_dir = r"C:\OPR"
my_zip = r"C:\OPR\109521P.zip"
with zipfile.ZipFile(my_zip) as zip_file:
for member in zip_file.namelist():
filename = os.path.basename(member)
# skip directories
if not filename:
continue
# copy file (taken from zipfile's extract)
source = zip_file.open(member)
target = open(os.path.join(my_dir, filename), "wb")
with source, target:
shutil.copyfileobj(source, target)
我得到了答案,只是發布它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.