简体   繁体   English

使用python将目录内的压缩文件夹中的所有文件提取到其他目录而不使用文件夹

[英]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.

相关问题 使用Python将所有压缩文件解压缩到相同目录 - Extract All Zipped Files to Same Directory Using Python 使用Python 2.7.5将文件夹中的所有压缩文件解压缩到同一文件夹 - Unzip all zipped files in a folder to that same folder using Python 2.7.5 如何使用python提取文件夹中的所有.zip扩展名而不保留目录? - How can I extract all .zip extension in a folder without retaining directory using python? zip 文件夹使用 python 而不更改目录 - zip folder using python without changing directory 如何解压缩所有以.zip结尾的文件夹/文件,并从每个压缩的文件夹中提取“ file.txt”文件 - How to unzip all folders/files that end in .zip and extract “file.txt” file from each zipped folder 如何从目录中的文件夹名称中提取后缀字符串? (蟒蛇) - How to Extract A String of the Suffix from Folder Names in a Directory? (Python) 如何将目录/文件夹中的所有pdf文件转换为图像python 3? - How to convert all pdf files in a directory/folder to image python 3? zip 包含所有内容的文件夹,但不保留 Python 中的目录结构 - zip a folder with all its content without preserving directory structure in Python 将文件夹目录中的所有.csv文件复制到python中的一个文件夹 - Copy all .csv files in a directory of folders to one folder in python 从带有子文件夹的压缩文件夹中提取.txt 文件并使用文件夹位置重命名文件 - Extract .txt files from zipped folder with subfolders and rename files with folder location
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM