Accessing the files by last modified time

S

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
 
C

Chris Rebert

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
 
T

Tim Williams

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()
 
P

Peter Otten

Sangeet said:
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)
 
N

Neil Cerutti

Check out os.stat()

I've been using os.path.getmtime, and converting that to a
datetime object using datetime.datetime.fromtimestamp.

Surprisingly, perhaps, this has been working.

According to the docs, to avoid making any assumptiong, I might
need to do:

tm = os.path.getmtime(apath)
lt = time.localtime(tm)
datetime.datetime(lt.tm_year, lt.tm_mon, lt.tm_mday)

Here's where I'm confused about the functioning of my original
plan. The docs say:

os.path.getmtime
[...] The return value is a number giving the number of seconds
since the epoch (see the time module). [...]

classmethod datetime.fromtimestamp
Return the local date and time corresponding to the POSIX
timestamp, such as returned by time.time(). [...]

time.time
Return the time as a floating point number expressed as seconds
since the epoch, in UTC. [...]

I'm not completely sure the return type of getmtime and the
argument type of fromtimestamp are really the same or not. I
guess I came to that conclusion some time in the past, and it
does seem to work. It may be a simple case of just different
aspects the exact same type being being highlighted in each
definition.
 
S

Sangeet

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

Thanks Chris, this helped me a lot!
 
S

Sangeet

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

Thanks Chris, this helped me a lot!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top