1

How can you compare 2 dates in python when one is a str object from os.path.getatime() I have stripped the getatime() object down to just the month, day and year, and converted it to a datetime object but I throw a value error.

import os
from time import ctime, strftime
import datetime

folder = r'C:\users\c\desktop\files'
today = strftime("%Y-%m-%d")
present = datetime
files = os.listdir(folder)
for file in files:
    if file.endswith(".torrent"):
        # Get date time from each file then join file and folder to create full path
        date = ctime(os.path.getatime(os.path.join(folder, file)))
        # Remove the timestamp
        remove_timestamp = date.replace(date[11:19], "")
        # Remove the day
        new_date = remove_timestamp.replace(remove_timestamp[0:4], "")
        # print(new_date)
        if " " in new_date:
            # Replace space with a comma
            x = new_date.replace(" ", ",")
            if ",," in x:
                # Replace 2 commas with 1 coma
                z = x.replace(",,", ",")
                if "Mar" in z:
                    # Change month name to a number                        
                    mar = z.replace("Mar", "03")
                    # Problem start here
                    two_weeks = datetime.datetime(int(mar))
                    print(two_weeks)
                    # /Problem
                    if mar[4] is ",":
                        print(" " + mar, file)
                    else:
                        print(mar, file)
                if "Feb" in z:
                    # Change month name to a number
                    feb = z.replace("Feb", "02")
                    if feb[4] is ",":
                        print(" " + feb, file)
                    else:
                        print(feb, file)

Error

C:\Python34\python.exe C:/Users/c/PycharmProjects/untitled10/main.py
03,19,2015 some_file.torrent
Traceback (most recent call last):
  File "C:/Users/c/PycharmProjects/untitled10/main.py", line 30, in <module>
    two_weeks = datetime.datetime(int(mar))
ValueError: invalid literal for int() with base 10: '03,19,2015'

Process finished with exit code 1
Shilo
  • 313
  • 3
  • 16
  • 1
    Could you add the error message ? as it could help into assisting you – dvhh Mar 22 '15 at 04:45
  • ValueError: invalid literal for int() with base 10: '03,19,2015' – Shilo Mar 22 '15 at 04:50
  • Please put that error message in the question (preferably with the stack trace ) – dvhh Mar 22 '15 at 04:54
  • Traceback added to original post – Shilo Mar 22 '15 at 05:02
  • So if I understood correctly you want to convert the `date string` to an `int`, using `int()` and it is not working because the string is in an invalid format, why are you using `ctime` if you want roughtly 24h hours precision you could divide by ~24*3600 (give or take a few seconds ) and truncate to an int ? – dvhh Mar 22 '15 at 05:03

2 Answers2

2

os.path.getatime() returns "seconds since epoch" as a float number (the value returned by time.time() function). It is a POSIX timestamp ("seconds since Epoch" -- the number of non-leap SI seconds elapsed since 1970-01-01 UTC) on common systems where Python works (unless "right" timezone is used).

To convert it to a datetime object that represents time in UTC, call datetime.utcfromtimestamp() method.

Do not confuse a string representation of an object and the object itself. Do not compare time/date as strings, use objects instead.

from datetime import datetime, timedelta

now = datetime.utcnow()
file_time = datetime.utcfromtimestamp(os.path.getmtime(filename))
if (now - file_time) > timedelta(1): 
    print("more than a day has passed")

See Find if 24 hrs have passed between datetimes - Python.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

As os.path.getatime return a timestamp wouldn't it be better to either use a modulo with (24%3600) to roughly get the date up to a day precision.

If you still want to convert it to a date string then compare each component you could use time.strptime(string[, format]) which should parse the string into a tuple of the date components.

dvhh
  • 4,724
  • 27
  • 33