LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 04: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 04:30 AM.
 
Old 11-06-2014, 09:49 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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, 07: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



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 11:34 AM
[SOLVED] bash: Convert decimal to minutes:seconds? DJCharlie Programming 10 01-26-2011 07:05 PM
[SOLVED] Using convert from imagemagick package to convert multiple jpg files janis_169 Linux - Newbie 5 07-18-2010 04: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 05:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:20 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
Open Source Consulting | Domain Registration