471,330 Members | 1,634 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

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

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!

Sep 20 '07 #1
3 19002
On Sep 20, 5:31 pm, junchi.t...@gmail.com wrote:
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...

Sep 20 '07 #2
ju*********@gmail.com wrote:
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)
Sep 20 '07 #3
ju*********@gmail.com wrote:
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)
Sep 20 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Richard Hollenbeck | last post: by
6 posts views Thread by kimes | last post: by
34 posts views Thread by Reinhold Birkenfeld | last post: by
9 posts views Thread by Rich | last post: by
2 posts views Thread by Rob Cowie | last post: by
5 posts views Thread by Jorg Rødsjø | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.