使用python对电脑资料进行整理

首先通过bat文件,在磁盘根目录下得到所有的文件,保存在txt中,如下所示

cd /d %~dp0
dir /b/s *.*>目录.txt

 

注意:保存的txt文件,是ascii编码的,接下来使用python脚本可正常处理,若为其他编码,出现gbk' codec can't decode byte 0xae in position 537: illegal multibyte sequence,这样的错误


接着,通过以下python脚本,将所有需要整理的资料后缀,按后缀名生成txt


import re
import sys
import os
import datetime
import chardet




ListBookFileType=['\.doc','\.docx','\.ppt','\.pptx','\.xls','\.xlsx','\.caj','\.pdf','\.epub','\.chm','\.mobi','\.azw','\.azw3','\.djv','\.djvu','\.gv','\.tex','\.dot']
ListBookflag=[0]*len(ListBookFileType)#计数
ListFile=[0]*len(ListBookFileType)#文件名
ListBookFile=[0]*len(ListBookFileType)#打开文件的对象list
bookpath=os.getcwd()
contentName=bookpath+'\\book.txt'


txtFileObj=open(contentName,encoding='gb18030')#https://blog.csdn.net/shijing_0214/article/details/51971734

#使用gb18030,出现错误几率小些

#print(txtFileObj)
for j in txtFileObj:
    print(j)
    j=str(j)
    j=j.replace("\\\\","\\")
    j=j.replace('\\r\\n','')
    for item in ListBookFileType:
        if re.search(item,j,0) is None:
            #未搜索到
            continue
        else:
            if ListBookflag[ListBookFileType.index(item)]==0:
                ListFile[ListBookFileType.index(item)]=bookpath+'\\'+item[2:len(item)]+'.txt'
                ListBookFile[ListBookFileType.index(item)]=open(ListFile[ListBookFileType.index(item)],'w')
                ListBookFile[ListBookFileType.index(item)].write(j)
                ListBookflag[ListBookFileType.index(item)]=ListBookflag[ListBookFileType.index(item)]+1
            else:
               ListBookflag[ListBookFileType.index(item)]=ListBookflag[ListBookFileType.index(item)]+1
               ListBookFile[ListBookFileType.index(item)].write(j)
for item1 in ListBookFile:
    if not ListBookflag[ListBookFile.index(item1)]==0:
        item1.close()     

你可能感兴趣的:(toolbox)