Click on " Python crawler and data mining " above to follow
Reply to " Books " to receive a total of 10 e-books of Python from beginner to advanced
now
day
Chickens
soup
Feeling closer to hometown is even more timid, and dare not ask anyone.
I. Introduction
Hi everyone, this is Cui Yanfei. When receiving help from the project, I need to compress files in thousands of folders, and delete the source files, only keep the compressed files after compression. The amount of data is large, and manual completion is time-consuming and labor-intensive, and it is more appropriate to use Python for processing. Up.
2. Project goals
Compress the contents of folders in batches to meet customer requirements.
3. Project preparation
Software: PyCharm
Required libraries: os, shutil, zipfile
Four, project analysis
1) How to read the source file?
Use the OS library to get the folder name list, and use the for loop to easily get the source file to be compressed.
2) How to perform compression processing?
Use zipfile.ZipFile() in the zipfile library to compress the acquired files.
3) How to delete source files?
First use the remove() of the os library to delete files, and then use the rmtree() of the shutil library to delete empty folders.
Five, project realization
1. The first step is to import the three libraries needed
import os as os
import shutil
import zipfile
2. The second step defines the delete file function and the compressed file function
def del_(rootdir):
filelist = []
filelist = os.listdir(rootdir) # 列出该目录下的所有文件名
for f in filelist:
filepath = os.path.join(rootdir, f) # 将文件名映射成绝对路劲
if os.path.isfile(filepath): # 判断该文件是否为文件或者文件夹
os.remove(filepath) # 若为文件,则直接删除
elif os.path.isdir(filepath):
shutil.rmtree(filepath, True) # 若为文件夹,则删除该文件夹及文件夹内所有文件
shutil.rmtree(rootdir, True)
def zipDir(dirpath,outFullName):
zip = zipfile.ZipFile(outFullName,"w",zipfile.ZIP_DEFLATED)
for path,dirnames,filenames in os.walk(dirpath):
# 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
fpath = path.replace(dirpath,'')
for filename in filenames:
zip.write(os.path.join(path,filename),os.path.join(fpath,filename))
zip.close()
3. The third step is to create the main function
def main():
path_end = 'D:/a/h/'
date= os.listdir(path_end)
# 获取目标文件夹所有文件夹名列表
for f in date:
ljbc='D:/a/h/'+f+'/'+'查询信息.zip'
ljbc2 = 'D:/a/h/' + f + '/' + '下发修改.zip'
#以上两行是创建压缩后的文件名
ljcx='D:/a/h/'+f+'/查询信息'
ljxf = 'D:/a/h/' + f + '/下发修改'
#以上两行是要压缩的源文件
zipDir(ljcx,ljbc)
zipDir(ljxf, ljbc2)
del_(ljcx)
del_(ljxf)
if __name__ == '__main__':
main()
Six, effect display
1. The processed folder:
2. Compressed files in the processed folder:
Seven, summary
This article introduces how to use Python to perform batch compression processing on a large number of files. In fact, it can be achieved with a few lines of statements. After the program is written, it can complete tasks that one person cannot complete in one day in less than one minute. Life is short, use Python!
Finally, those who need the project code of this article, please reply to the keyword " file compression " in the background of the official account to obtain it. If you encounter any problems during the operation, please feel free to leave a message or add a friend to the editor. The editor will help if you see it. Everyone solve the bug!
------------------- End -------------------
Recommendations of previous wonderful articles:
Django website combat-hand in hand with everyone to make a classic website
Teach you how to use Pycharm to connect to a remote Python environment
Teach you how to deploy web crawlers with Scrapy+Gerapy
Welcome everyone to like , leave a message, forward, reprint, thank you for your company and support
If you want to join the Python learning group, please reply in the background [ Enter the group ]
Thousands of rivers and mountains are always in love, can you click [ Looking ]
/Today's message topic/
Just say a word or two~~