Kirk ERW Kirk ERW - 1 month ago 12
Python Question

Extract date from filename

I have files named in the format gtYYMMDD.txt like for example gt130422.txt, I'd like to extract the date from this file name in the format 2013/04/22.

I'd like to precise that my script, when I execute it, it asks only for the date:

filetoread = "/home/scripts/gt"
raw_input("Enter date, please. For example, 130415: ") + ".txt"

So the date I want to get is a function of what the user types.

>>> from datetime import datetime
>>> filetoread = "/home/scripts/gt/"
>>> s = raw_input("Enter date please e.g: 130415: ")
Enter date please e.g: 130415: 130422
>>> date = datetime.strptime(s, '%y%m%d')
>>> print date.strftime("%Y/%m/%d")
>>> path = filetoread + s + ".txt"