473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Chronological Processing of Files

This feels like a stupid question but I'll ask it anyway.

How can I process files chronologically (newest last) when using
os.walk()?

Sep 21 '05 #1
6 2618
yoda wrote:
This feels like a stupid question but I'll ask it anyway.

How can I process files chronologically (newest last) when using
os.walk()?


Do you want the ordering to apply just to files within each directory,
or to all the files found (recursively) during the entire walk? Define
"newest" (most recent modified date or something else?). Is there any
reason why sorting with the results of os.access().st_mtime as the key
is not possible or sufficient?

-Peter
Sep 21 '05 #2
untested, ugly, but something like this would sort all the files in the
directory on os.path.getctime (not using os.walk() though). I'm sure
there is probably better ways to do it :)

filelist = []

def walkdir(currdir):
for files in os.listdir(currdir):
path = os.path.join(currdir, files)
if not os.path.isdir(path):
filelist.append([os.path.getctime(path), path])
else:
walkdir(path)

walkdir(r'c:\somedirectory')

filelist.sort()

for item in filelist:
dosomething(item[1])

dosomething is whatever function to process the files

Sep 21 '05 #3
yoda wrote:
This feels like a stupid question but I'll ask it anyway.

Definitely not a stupid question.
How can I process files chronologically (newest last) when using
os.walk()?


Try this:

In [16]: file_list = [(os.stat(f)[8], f) for f in [os.path.join(i[0],
j) for i in os.walk("/home/jmjones/public_html") for j in i[2]]]

In [17]: file_list.sort()

In [18]: sorted_file_list = [f[1] for f in file_list]
I *think* os.stat()[8] is the modification time element...but this
should probably work for you. That first list comprehension looks like
a booger if you're not familiar with them. If you have any trouble with
it, just shoot a message back to the list and I'll decypher it for you.

- JMJ
Sep 21 '05 #4
"Peter Hansen" <pe***@engcorp.com> wrote:
yoda wrote:
This feels like a stupid question but I'll ask it anyway.

How can I process files chronologically (newest last) when using
os.walk()?


Do you want the ordering to apply just to files within each directory,
or to all the files found (recursively) during the entire walk? Define
"newest" (most recent modified date or something else?). Is there any
reason why sorting with the results of os.access().st_mtime as the key
is not possible or sufficient?

-Peter


You can make your life easier using the non-standard (yet ?) path module:

from path import path

top = path('.')
sort_kwds = dict(key=path.mtime.__get__, reverse=True)

# sort all files together
sorted_all = sorted(top.walkfiles(), **sort_kwds)

# sort files by directory

sorted_by_dir = sorted(top.files(), **sort_kwds) \
+ sum((sorted(dir.files(), **sort_kwds)
for dir in path(top).walkdirs()), [])

George
Sep 22 '05 #5
Just to clarify:

Newest== modified last

The processing\sorting should apply to all the files found recursively
during the entire walk.

That being said, thanks for all the responses. I'll test the code
shortly and get back to everyone.

ps. This is why comp.lang.python is truly the greatest list ever.
(better than comp.lang.lisp?) Everyone is so helpful. Thanks again guys.

Sep 22 '05 #6
I've tried using the path module and it works like a *charm*.. plus my
code is cleaner and clearer.. :)

The list comprehension using os.stat() works well though I had to call
an additional reverse() on the resultant list so that I could get the
list in order of "newest first".

So, in conclusion, I'll use the path module.

Thanks again guys. You've been a great help.

Sep 26 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ganapathy | last post by:
I have COM dll code written in VC 6.0. When i tried compiling this code in VC 7, The MIDL cmpiler gets called twice. i.e. it initially compiles fully & immediately a line - 64 bit processing'...
2
by: r2d3 | last post by:
Hello, New to Oracle (9i), so hope some one can understand and help with this. I want to add a episode number to a table, which will give the sequence of each episode. (Sorry for using the word...
1
by: Xah Lee | last post by:
Text Processing with Emacs Lisp Xah Lee, 2007-10-29 This page gives a outline of how to use emacs lisp to do text processing, using a specific real-world problem as example. If you don't know...
0
by: Ed prochak | last post by:
rob.boler@milton-keynes.gov.uk (Rob Boler) wrote in message news:<6333cbba.0308260645.54ef2f32@posting.google.com>... I commend your desire to really understand your tools. I think you will find...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.