Check if file line sttime is older than 2 days - Python
Question: I have a series of entries in a text file like this:

    
    
    20150217_00:47:32 - AAAAAA
    20150217_00:47:32 - BBBBBB
    20150217_00:47:32 - CCCCCC
    

I want to make a function that will periodically read each line in the file
and do something depending on whether the entry is less than 2 days old, more
than 2 days old, or more than 7 days old.

I'm not sure how to get the code to understand the sttime timestamp in the
file though. My code (so far) is as follows:

    
    
    with open('entries.txt', 'r+') as entries:
            for line in entries:
                lineitem = line.strip()
                print lineitem[:17]
    

That retrieves the timestamps ok, but how to read them and interpret them as
times I have no idea :/

(This will end up in an if loop eventually that does the function described
above, but first I just need to know how to read those times...)


Answer: This will give you a
[datetime](https://docs.python.org/2/library/datetime.html) object which you
can [compare](https://docs.python.org/2/library/datetime.html#timedelta-
objects) against other datetime objects:

    
    
    from datetime import datetime, timedelta
    ...
    

~~linets = datetime.strptime('%Y%m%d_%H:%M:%s', line.strip()[:17])~~

Oops! Mark got those arguments the wrong way around. It should be

    
    
    linets = datetime.strptime(line.strip()[:17], '%Y%m%d_%H:%M:%S')
    

**test**

    
    
    >>> print datetime.strptime('20150217_00:47:32', '%Y%m%d_%H:%M:%S')
    2015-02-17 00:47:32
    

