文件夹下文件排序并转换为txt文件

83 篇文章 3 订阅
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 14 22:12:00 2019

@author: Administrator
"""
import os
from PIL import Image
import pytesseract
import time

while True:
    path="F:\\PythonWebPYCode\\mma\\static"
    
    def get_file_list(file_path):
        dir_list = os.listdir(file_path)
        if not dir_list:
            return
        else:
            # 注意,这里使用lambda表达式,将文件按照最后修改时间顺序升序排列
            # os.path.getmtime() 函数是获取文件最后修改时间
            # os.path.getctime() 函数是获取文件最后创建时间
            dir_list = sorted(dir_list,  key=lambda x: os.path.getctime(os.path.join(file_path, x)))
            # print(dir_list)
            return dir_list
        
    listS = get_file_list(path)
    
    print("all files")
    print(listS)
    
    #上面都是导包,只需要下面这一行就能实现图片文字识别
    #text=pytesseract.image_to_string(Image.open('333.jpeg'),lang='chi_sim')
    I=[]
    II=[]
    for s in listS:
        if 'txt' in s:
            II.append(s)
            continue
        if 'jpeg' in s:
            I.append(s)
    
    print("file name")
    print(I)
    print(II)
    num=len(II)-len(I)
    
    if num==0:
        print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
        time.sleep(1)
    else:
        Nnum=num
        print(num)
        print("How mash  times....")
        
        print("%%%%%%%%%%%%%%%%%%%%%")
        print(len(I))
        print(len(II))
        print("%%%%%%%%%%%%%%%%%%%%%")
        for n in range(len(I)-len(II)):
            print(n)
            print("translation")
            print(num)
            FilePath=os.path.join(path,I[num])
            print(FilePath)
            print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
            NewFile=FilePath+'.txt'
            print(NewFile)
            num=num+1    
            text=pytesseract.image_to_string(Image.open(FilePath),lang='chi_sim')
            with open(NewFile,"w",encoding='utf-8')as f:
                f.write(text)
            f.close()

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ a l l f i l e s [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ , ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] f i l e n a m e [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ ] [ ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] all files ['1.jpeg', '2.jpeg', '3.jpeg', '1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] file name ['1.jpeg', '2.jpeg', '3.jpeg'] ['1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] allfiles[1.jpeg,2.jpeg,3.jpeg,1.jpeg.txt,2.jpeg.txt,3.jpeg.txt]filename[1.jpeg,2.jpeg,3.jpeg][1.jpeg.txt,2.jpeg.txt,3.jpeg.txt]$$$$$$$$$$$$$$$$$$$$$$$$$$$ a l l f i l e s [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ , ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] f i l e n a m e [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ ] [ ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] all files ['1.jpeg', '2.jpeg', '3.jpeg', '1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] file name ['1.jpeg', '2.jpeg', '3.jpeg'] ['1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] allfiles[1.jpeg,2.jpeg,3.jpeg,1.jpeg.txt,2.jpeg.txt,3.jpeg.txt]filename[1.jpeg,2.jpeg,3.jpeg][1.jpeg.txt,2.jpeg.txt,3.jpeg.txt]$$$$$$$$$$$$$$$$$$$$$$$$$$$ a l l f i l e s [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ , ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] f i l e n a m e [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ ] [ ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] all files ['1.jpeg', '2.jpeg', '3.jpeg', '1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] file name ['1.jpeg', '2.jpeg', '3.jpeg'] ['1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] allfiles[1.jpeg,2.jpeg,3.jpeg,1.jpeg.txt,2.jpeg.txt,3.jpeg.txt]filename[1.jpeg,2.jpeg,3.jpeg][1.jpeg.txt,2.jpeg.txt,3.jpeg.txt]$$$$$$$$$$$$$$$$$$$$$$$$$$$ a l l f i l e s [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ , ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] f i l e n a m e [ ′ 1. j p e g ′ , ′ 2. j p e g ′ , ′ 3. j p e g ′ ] [ ′ 1. j p e g . t x t ′ , ′ 2. j p e g . t x t ′ , ′ 3. j p e g . t x t ′ ] all files ['1.jpeg', '2.jpeg', '3.jpeg', '1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] file name ['1.jpeg', '2.jpeg', '3.jpeg'] ['1.jpeg.txt', '2.jpeg.txt', '3.jpeg.txt'] allfiles[1.jpeg,2.jpeg,3.jpeg,1.jpeg.txt,2.jpeg.txt,3.jpeg.txt]filename[1.jpeg,2.jpeg,3.jpeg][1.jpeg.txt,2.jpeg.txt,3.jpeg.txt]$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值