473,378 Members | 1,620 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,378 software developers and data experts.

Process files in order

Hi,

I have a requirement to process all files in a directory in
chronological order. The os.listdir() function, however, lists the
files in random order. Is there a similar function in Python that
allows me to specify the listing order (like ls -t for example)?

Thanks,
Khoa
Jul 27 '06 #1
4 3104
Khoa Nguyen wrote:
I have a requirement to process all files in a directory in
chronological order. The os.listdir() function, however, lists the
files in random order. Is there a similar function in Python that
allows me to specify the listing order (like ls -t for example)?
There is no single command, but you can easily sort the results of
listdir using any criteria. Most file attributes can can be obtained
using os.stat (size, creation date, modification date, etc), and you can
just use that as a key to sort().
Jul 27 '06 #2
How about using os.listdir to build a list of filenames, then sorting
them by modification time (via os.stat)?

Jul 27 '06 #3
Khoa Nguyen a écrit :
Hi,

I have a requirement to process all files in a directory in
chronological order.
Access time, modification time ? Ascending, descending ?-)
The os.listdir() function, however, lists the
files in random order. Is there a similar function in Python that
allows me to specify the listing order (like ls -t for example)?
Not AFAIK. But os.path.get[acm]time(<filename>) and sorted() may help:

from os.path import getmtime, join, isfile
from os import listdir, getcwd

listfiles = lambda p: filter(isfile, # only list files
map(lambda f, p=p : join(p,f),
listdir(p)))

files = listfiles(getcwd())
sortedfiles = map(lambda item: item[1],
sorted(zip(map(getmtime, files), files)))
You can apply reversed() to sortedfiles if you want them in reversed order.
HTH
Jul 27 '06 #4
A possibility:

import os
_, _, file_names = os.walk("").next()
print sorted(file_names, key=lambda fn: os.stat(fn)[8])

Bye,
bearophile

Jul 27 '06 #5

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

Similar topics

2
by: RL | last post by:
Hello Perl gurus, 1. I have a web page where I can push a button (dospawn.html). 2. This button calls a CGI script (spawnboss.cgi) 3. spawnboss.cgi calls a forking perl script (forkme.pl) 4....
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
3
by: dave | last post by:
Hi, Does anyone know how I could make an .exe launched server side from an aspx file run faster? When I use javascript client side it of couse is much faster. Here's my code This code does...
3
by: Trevor Andrew | last post by:
Hi There, I have a small ASP.NET application under development. I am using VS.NET 2002 (2003 upgrade is on the way) with .NET Framework 1.1. It is hosted on a web hosting service in the US. I am...
4
by: Jerry | last post by:
I've inherited a system w/5 projects - I'll refer to them as P1 thru P5 - P5 being the executable... P1 has no dependents. P2 is dependant upon P1 P3 has no dependents. P4 is dependant upon P1,...
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
3
by: Steve B. | last post by:
Hi, I'm written a Web Service that encapsulate the call to a command line application. The command line application is run using Process.Start, and I'd like to get both standard ouput and...
11
by: garyusenet | last post by:
For this first time today I used the System.Diagnositcs namespace to launch a program from my c# code. The program launches OK but I have something which has completely stumped me. The...
3
by: Hartmut Dippon | last post by:
Hi all, I hope somebody can help me with following problem: I have an application where I can drag&drop files/dirs from within explorer onto my form. If multiple files/dirs are selected I...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.