Python forum for Python programmers

Accessing the files by last modified time

Mar 22, 2012 11:33 am
Sangeet

Hi

I am new to the python programming language.

I've been trying to write a script that would access the last modified file in one of my directories. I'm using Win XP.

I saw a similar topic, on the forum before, however the reply using (os.popen) didn't work out for me. I'm not sure whether it was due to syntax errors either.

Thanks,
Sangeet

Mar 22, 2012 11:49 am
Chris Rebert
Re: Accessing the files by last modified time

On Thu, Mar 22, 2012 at 4:33 AM, Sangeet <mrsangeet@gmail.com> wrote:
> Hi
>
> I am new to the python programming language.
>
> I've been trying to write a script that would access the last modified file in one of my directories. I'm using Win XP.
>
> I saw a similar topic, on the forum before, however the reply using (os.popen) didn't work out for me. I'm not sure whether it was due to syntax errors either.

Recursively or non-recursively?
Live monitoring or just one-time?
What was the forum post in question?

In the simple case, you just need to stitch together these functions:
http://docs.python.org/library/os.html#os.stat
(note the .st_mtime attribute of the result)
http://docs.python.org/library/os.path.html#os.path.join
with one of these functions:
http://docs.python.org/library/os.html#os.listdir
http://docs.python.org/library/os.html#os.walk

Cheers,
Chris


Mar 22, 2012 12:04 pm
Tim Williams
Re: Accessing the files by last modified time

On Mar 22, 7:33 am, Sangeet <mrsang...@gmail.com> wrote:
> Hi
>
> I am new to the python programming language.
>
> I've been trying to write a script that would access the last modified file in one of my directories. I'm using Win XP.
>
> I saw a similar topic, on the forum before, however the reply using (os.popen) didn't work out for me. I'm not sure whether it was due to syntax errors either.
>
> Thanks,
> Sangeet

Check out os.stat()


Mar 22, 2012 12:38 pm
Peter Otten
Re: Accessing the files by last modified time

Sangeet wrote:

> I've been trying to write a script that would access the last modified
> file in one of my directories. I'm using Win XP.

import os
import glob

path = r"c:\one\of\my directories\*"

youngest_file = max(glob.glob(path), key=os.path.getmtime)





Previous Thread: Best way to disconnect from ldap?
Next Thread: Number of languages known [was Re: Python is readable] - somewhat OT

Related Forum Topics
Help accessing COM .dll from Python
<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<font size="-1"><font face="Bitstream Vera Sans">First time post -
be gentle wi<font size="-1">th me :-)<br>
<br>
...
Modifying a time.struct_time
Hi!

I'm trying to create a struct_time that is e.g. one year ahead or a
month back in order to test some parsing/formatting code with different
dates.

Now, the straightforward approach is

t = time.localtime()
t.tm_year += 1

This fails with "TypeError: readonly attribute"....
PyHook and time libraries
Hey, I'm tring to create a software that records the keyboard/mouse and sends email of the log every predetermined period.

I've manage to make the recorder and the auto-email sender, but I still can't make both of them work simultaneously.

Can someone help me with this please?
I thought...
Problem with eval and time
Dear all, I would like to convert tstr to representation of time, but encounter the following error. Is there a simple way to get what I want? Thanks.

>>> import time
>>> tstr = str(time.localtime())
>>> eval(tstr)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
...
Average time calculation??
Hi there guys i've got a script that's suppose to find the average of two times as strings. The times are in minutes:seconds:milliseconds
i'm doing ok in printing the right minutes and seconds my problem is with the milliseconds.

Example if i have 00:02:20 and 00:04:40 the average will be...
Real time event accuracy
I'd like to send MIDI events from python to another
program. I'd like advice as to how to accurately
time the events. I'll have a list of floating point
start times in seconds for the events, and I'd like to send them
off as close to the correct time as possible.

I'd also appreciate...
Limited time offer, so hurry up!
Take advantage of Smart Baba new offer and get your own customized
professional website and promote your business. For further details,
Follow us:

www.websitedeals.in


Why does dead code costs time?
Hi,

I'm interested in compilers optimizations, so I study python compilation process

I ran that script:

import timeit

def f(x):
return None

def g(x):
return None
print(x)

number = 10000

print(timeit.timeit('f(1)',setup="from...
An object is and isn't an instance of a class at the same time
I was debugging some code using isinstance() to make sure the correct object was been passed to the method and came across something that is really ticking me off.

I have a class called 'Jitem' in its own file called 'jitem.py'. It's part of a package called 'jukebox'. I also have '__all__'...
PyWarts: time, datetime, and calendar modules
The interface for these modules is not intuitive. Instead of creating
true OOP objects we have lists and strings. Any calendar object should
expose string names of both: days of the week and months of the year.
It seems one (or possibly more) of the three expose this important
info however i...