根据创建日期选择文件

时间:2019-01-31 11:38:06

标签: python

我有一个名为myclientcard的文件夹,它有69个子文件夹,在这些子文件夹中,我们有多个子文件夹,该文件夹必须转到错误文件夹,而在错误文件夹中,它具有多个txt文件,所以我想要那些文本文件的内容使用日期格式17/01/201924/01/2019指定的内部错误中的所有69个文件夹中的所有文件夹,并将其转换为excel文件

import os
import numpy as np
from os import listdir
from os.path import join
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
mypath = "D:\myclientcard"
files = [join(mypath,f) for f in listdir(mypath) if '.txt' not in f]
for file in files:
    path = file
    filename =[join(path,f) for f in listdir(path) if 'ERROR' in f]
    #print(filename)
    for text_file_path in filename:
        file_path = text_file_path
        textfiles = [join(file_path,f) for f in listdir(file_path) if '.txt' in f]
        for files in textfiles:
                reading_files = open(files,'r')
                read = reading_files.read()
                writting_files = open('result.txt','a')
                wr = writting_files.write(read)
                read_files = pd.read_csv('result.txt',delim_whitespace='')
                writer = ExcelWriter('output.xlsx')
                read_files.to_excel(writer,'Sheet1',index=false)
                writer.save()
                reading_files.close()
                writting_files.close()

1 个答案:

答案 0 :(得分:0)

使用herehere的答案。假设您在Windows平台上。

import os
import numpy as np
from os import listdir
from os.path import join
# Importing datetime module
from datetime import datetime as dt
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
mypath = "D:\myclientcard"

# Add start date here
start_date = dt.strptime('17/01/2019', '%d/%m/%Y')
# Add end date here
end_date = dt.strptime('24/01/2019', '%d/%m/%Y')
files = [join(mypath,f) for f in listdir(mypath) if '.txt' not in f]
for file in files:
    path = file
    filename =[join(path,f) for f in listdir(path) if 'ERROR' in f]
    #print(filename)
    for text_file_path in filename:
        file_path = text_file_path
        textfiles = [join(file_path,f) for f in listdir(file_path) if '.txt' in f]
        # Filtering on the basis of date
        textfiles = [f for f in textfiles if ((os.path.getctime(f) >= start_date) and (os.path.getctime(f) <= end_date))]
        for files in textfiles:
                reading_files = open(files,'r')
                read = reading_files.read()
                writting_files = open('result.txt','a')
                wr = writting_files.write(read)
                read_files = pd.read_csv('result.txt',delim_whitespace='')
                writer = ExcelWriter('output.xlsx')
                read_files.to_excel(writer,'Sheet1',index=false)
                writer.save()
                reading_files.close()
                writting_files.close()

另一方面,请考虑优化代码。也可以尝试os.walk,它有时会很有用!