我想从zip文件中提取txt文件到桌面,我用下面的代码获得了它,但是如果zip文件有包含文本文件的文件,我如何从文件中提取这些文本文件到桌面?
import zipfile
def abc(path_to_zip_file, directory_to_extract_to):
with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:
zip_ref.extractall(directory_to_extract_to)
path_to_zip_file ="zip file path"
directory_to_extract_to = "Desktop path"
z = zipfile.ZipFile(path_to_zip_file)
abc(path_to_zip_file, directory_to_extract_to)发布于 2021-07-26 22:27:54
import zipfile, os, shutil
myZip = zipfile.ZipFile('visualvm_207.zip')
for file in myZip.filelist:
if file.filename.endswith('.txt'):
source = myZip.open(file)
target = open(os.path.join(directory_to_extract_to, os.path.basename(file.filename)), "wb")
with source, target:
shutil.copyfileobj(source, target)https://stackoverflow.com/questions/68531506
复制相似问题