os.path.getmtime() and compare with a date type

J

junchi.tang

Hi,
I am new to python and are tryint to write a simple program delete log
files that are older than 30 days.

So I used os.path.getmtime(filepath) and compare it with a date but it
does not compile.

threshold_time = datetime.date.today() - datetime.timedelta(days=30)
mod_time = os.path.getmtime(file_path)

if( mod_time < threshold_time):
#delete file

However the interpreter complains at the if line, say "can't comapre
datetime.date to int

How can I covert one of them to make it work?

Thank you!
 
C

cokofreedom

Hi,
I am new to python and are tryint to write a simple program delete log
files that are older than 30 days.

So I used os.path.getmtime(filepath) and compare it with a date but it
does not compile.

threshold_time = datetime.date.today() - datetime.timedelta(days=30)
mod_time = os.path.getmtime(file_path)

if( mod_time < threshold_time):
#delete file

However the interpreter complains at the if line, say "can't comapre
datetime.date to int

How can I covert one of them to make it work?

Thank you!

would putting it within int() work? I've not had much experience with
time module, but I guess it must have a similar function...
 
A

Anthony Greene

Hi,
I am new to python and are tryint to write a simple program delete log
files that are older than 30 days.

So I used os.path.getmtime(filepath) and compare it with a date but it
does not compile.

threshold_time = datetime.date.today() - datetime.timedelta(days=30)
mod_time = os.path.getmtime(file_path)

if( mod_time < threshold_time):
#delete file

However the interpreter complains at the if line, say "can't comapre
datetime.date to int

How can I covert one of them to make it work?

Thank you!
You are looking for datetime.datetime.fromtimestamp(mod_time)
 
A

Anthony Greene

Hi,
I am new to python and are tryint to write a simple program delete log
files that are older than 30 days.

So I used os.path.getmtime(filepath) and compare it with a date but it
does not compile.

threshold_time = datetime.date.today() - datetime.timedelta(days=30)
mod_time = os.path.getmtime(file_path)

if( mod_time < threshold_time):
#delete file

However the interpreter complains at the if line, say "can't comapre
datetime.date to int

How can I covert one of them to make it work?

Thank you!
You are looking for datetime.datetime.fromtimestamp(mod_time)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top