I have a script that runs every 30 minutes on my server, and when it's done it writes a timestamp to the last line of the file. It does this to about 20+ files in one directory. I'm trying to write a Python script to check the last line of each file and return either a "In progress" or "Finished at:" message for each file depending on if that timestamp is present or not.
How do I get Python to determine if a string is a timestamp or not? It is formatted as HH:MM:SS, no month/date indications.
You could try to match a regular expression:
import re
if re.match('\d{2}:\d{2}:\d{2}', line):
would work if the line starts with 3 2-digit pairs with colons in between; depending on what else you might find in line this could easily be enough.
Another option would be to try and parse it:
import time
try:
time.strptime(line[:8], '%H:%M:%S')
except ValueError:
print('Not a timestamp')
else:
print('Found a timestamp')
This is a lot stricter; a ValueError is thrown whenever a line does not start with a timestamp that can actually represent a valid time value, so the hour must be in the range 0-23, minutes in the range 0-59 and seconds in the range 0-60.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With