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