472,992 Members | 3,500 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 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 19408
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
6
by: kimes | last post by:
I've just started digging into how python works.. I found that other mudules are clearly declared like one file per a module.. But the only os.path doesn't have their own file.. ye I know is...
34
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
9
by: Rich | last post by:
Thanks for the Help in my previous post. I've been working on this and it's almost what I want. I want to obtain the user's current age by comparing their date of birth (user inputs) to the...
2
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string...
5
by: Jorg Rødsjø | last post by:
Hi I'm trying to use os.path.getmtime to check if a file has been modified. My OS is WinXP. The problem is, that when the os changes from/to daylight savings time, the result is suddenly off by...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
5
by: Tom | last post by:
It appears that you can't compare two dates in DotNet. You must use ToString and compare the strings. Is that the only reliable way? Try this: Dim dteOne As Date =...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.