[英]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!')
I have around 10 images zipped inside a sub folder in a zipped folder , now i want to extract all the images directly to other directory without the sub folders , I have tried using os.path.basename(name) , but i'm getting severel errors.我有大约 10 张图像压缩在一个压缩文件夹的子文件夹中,现在我想将所有图像直接解压缩到其他目录而不使用子文件夹,我尝试使用 os.path.basename(name) ,但我得到了严重错误。
After the above code , I', getting all images inside a folder ,,在上面的代码之后,我将所有图像放入一个文件夹中,,
C:\\images\\109521P C:\\图像\\109521P
Above is the output location where all 10 images are being extracted , Now i want the images to be directly extracted at以上是提取所有10张图像的输出位置,现在我想直接提取图像
C:\\images C:\\图像
So i want to omit the sub folder 109521P and want the images to be directly extracted at above loaction.所以我想省略子文件夹 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)
I got the answer , just posting it我得到了答案,只是发布它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.