Python file concating in the same directory usinf endswith but in order of the timestamp

35 Views Asked by At

I was trying to concat all txt files in the directory while removing first rows that weren't needed but I don't seem to find a way of contacting the files in the order of timestamp, the filenames also represent the needed order.

import os

switch = True;
for file in os.listdir(os.getcwd()):
    if file.endswith(".txt"):
        print(file)
        with open(file, 'r') as fin:
            dt = fin.read().splitlines(True)
            with open(os.path.join(os.getcwd(), 'filename.txt'), 'a') as fout:
                if switch == True:
                    fout.writelines(dt[5:])
                    switch = False;
                elif switch == False:
                    fout.writelines(dt[5:])


output:

Sensors.20210209100733.txt
Sensors.20210209025203.txt
Sensors.20210209042850.txt
Sensors.20210209002654.txt
Sensors.20210209020340.txt
Sensors.20210209011517.txt
Sensors.20210208211320.txt
Sensors.20210209034027.txt
Sensors.20210208184810.txt
Sensors.20210208171124.txt
Sensors.20210208193634.txt
Sensors.20210209060537.txt
Sensors.20210208175947.txt
Sensors.20210209074223.txt
Sensors.20210209051713.txt
Sensors.20210208233830.txt
Sensors.20210209083047.txt
Sensors.20210209091910.txt
Sensors.20210208225007.txt
Sensors.20210208202457.txt
Sensors.20210208220144.txt
Sensors.20210209065400.txt

Needed order:

first file - Sensors.20210208171124 ..datetime order... last file - Sensors.20210209100733

Thank you!

1

There are 1 best solutions below

0
On

Assuming, you already have the file names in the list, you can use the sorted method with a custom method which sorts by date from name of the file as:

x = ["Sensors.20210209100733.txt", "Sensors.20210209025203.txt", 
     "Sensors.20210209042850.txt", "Sensors.20210209002654.txt",
     "Sensors.20210209020340.txt", "Sensors.20210209011517.txt",
     "Sensors.20210208211320.txt", "Sensors.20210209034027.txt",
     "Sensors.20210208184810.txt", "Sensors.20210208171124.txt",
     "Sensors.20210208193634.txt", "Sensors.20210209060537.txt",
     "Sensors.20210208175947.txt", "Sensors.20210209074223.txt",
     "Sensors.20210209051713.txt", "Sensors.20210208233830.txt"]

from dateutil.parser import parse

def date_from_name(name):
    return parse(name.split('.')[-2])
    
sorted_list = sorted(x, key=date_from_name)
print(sorted_list)

Output:

['Sensors.20210208171124.txt', 'Sensors.20210208175947.txt', 'Sensors.20210208184810.txt', 'Sensors.20210208193634.txt', 'Sensors.20210208211320.txt', 'Sensors.20210208233830.txt', 'Sensors.20210209002654.txt', 'Sensors.20210209011517.txt', 'Sensors.20210209020340.txt', 'Sensors.20210209025203.txt', 'Sensors.20210209034027.txt', 'Sensors.20210209042850.txt', 'Sensors.20210209051713.txt', 'Sensors.20210209060537.txt', 'Sensors.20210209074223.txt', 'Sensors.20210209100733.txt']