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

Discussion in 'Python' started by junchi.tang@gmail.com, Sep 20, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Guest

    On Sep 20, 5:31 pm, 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, 2007
    #2
    1. Advertising

  3. 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)
    Anthony Greene, Sep 20, 2007
    #3
  4. 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)
    Anthony Greene, Sep 20, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Qvx
    Replies:
    3
    Views:
    366
    Nick Coghlan
    Jan 29, 2005
  2. =?ISO-8859-1?Q?Jorg_R=F8dsj=F8?=

    os.path.getmtime on winXP

    =?ISO-8859-1?Q?Jorg_R=F8dsj=F8?=, Nov 8, 2005, in forum: Python
    Replies:
    5
    Views:
    4,584
    Bengt Richter
    Nov 8, 2005
  3. Josef Dalcolmo

    getmtime differs between Py2.5 and Py2.4

    Josef Dalcolmo, May 7, 2007, in forum: Python
    Replies:
    16
    Views:
    527
    Joe Salmeri
    Jun 1, 2007
  4. mynthon
    Replies:
    5
    Views:
    1,993
    krowey
    Feb 9, 2013
  5. Tim Johnson
    Replies:
    0
    Views:
    248
    Tim Johnson
    Sep 14, 2011
Loading...

Share This Page