使用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()     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值