LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-06-2014, 05:23 AM   #1
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Rep: Reputation: Disabled
convert st_mtime to minutes from now


hello all,

ive been trying to search for an answer but its so hard to find a proper answer, here is my problem. In a linux machine, i want to check for files age and remove them if they are older than 2 hours. but i can get the st_mtime but dont know how to convert that to "how may minutes from now". this is the python script i created so far :

Code:
#!/usr/bin/env python

import os
import glob
import commands
import time

location = "/usr/local/testlogs/text/*"

for file in glob.glob(location) :
    #print file
    st = os.stat(file)
    mtime = st.st_mtime
    mtime2 = time.gmtime(mtime)
    mtime2_hr = time.strftime("%m/%d/&Y %H:%M:%S", mtime2)
    print mtime2_hr
the output i get show's when the file was last modified in "%m/%d/&Y %H:%M:%S" format. how to get it to ONLY minutes from now format. please help im new to python

Last edited by simhumcon; 11-06-2014 at 05:30 AM.
 
Old 11-06-2014, 10:49 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,957

Rep: Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267Reputation: 1267
Why on Earth do you take a time stamp that is already in its simplest form (seconds since the epoch), laboriously convert it to the form least suitable for computation, and then try to do arithmetic on it? All you need to do is subtract st.st_mtime from the current time and divide by 60 to convert seconds to minutes.
Code:
age_minutes = (time.time() - st.st_mtime) / 60
 
Old 11-06-2014, 08:06 PM   #3
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
wow i didnt know we could to do that! thank you very much rknichols! im really new to python. thanks again
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert total no of seconds in the format hour minutes and seconds suchi_s Programming 15 03-15-2011 12:34 PM
[SOLVED] bash: Convert decimal to minutes:seconds? DJCharlie Programming 10 01-26-2011 08:05 PM
[SOLVED] Using convert from imagemagick package to convert multiple jpg files janis_169 Linux - Newbie 5 07-18-2010 05:22 PM
[SOLVED] X takes 1.5 minutes to start xterm and 3.5 minutes to start GNOME gregorian Linux - Newbie 16 12-25-2009 06:56 AM


All times are GMT -5. The time now is 03:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration