我有一个目录,其中包含不同名称的zip文件的列表。我想解压缩它们并将文件复制到它们各自的位置。
假设下面是我的目录位置:
F:\Download_Data\myData\
它具有以下一组zip文件:
ABC_EMPLOYEECODES20190513.txt.zip
ABC_EMPLOYEECODES20190516.txt.zip
ABC_EMPLOYEECODES20190521.txt.zip
ABC_EMPLOYEECODES20190528.txt.zip
和
ABC_EMPLOYEEDETAILS20190413.txt.zip
ABC_EMPLOYEEDETAILS20190416.txt.zip
我想阅读以上目录并解压缩/解压缩这些文件,并将其放置在相应的位置,具体取决于文件名:
ABC_EMPLOYEECODES20190513.txt
ABC_EMPLOYEECODES20190516.txt
ABC_EMPLOYEECODES20190521.txt
ABC_EMPLOYEECODES20190528.txt
给定的文件应转到以下位置:'F:\EmployeeCode\myData\'
在给定的文件下面,应转到此位置:'F:\EmployeeDetails\myData\'
ABC_EMPLOYEEDETAILS20190413.txt
ABC_EMPLOYEEDETAILS20190416.txt
这是我要执行任务的代码:
import zipfile
import os
zipfilepath="F:/Download_Data/myData"
unzipfilepath1 = "G:/testfolder1"
unzipfilepath2 = "G:/testfolder2"
for root,dirs,files in os.walk(zipfilepath):
for file in files:
if file.startswith("ABC_EMPLOYEECODES"):
filepath1=os.path.join(root,file)
with zipfile.ZipFile(filepath1,"r") as zip_ref:
zip_ref.extractall(unzipfilepath1)
elif file.startswith("ABC_EMPLOYEEDETAILS"):
filepath2=os.path.join(root,file)
with zipfile.ZipFile(filepath1,"r") as zip_ref:
zip_ref.extractall(unzipfilepath2)
现在工作正常。任何建议都非常欢迎。