listdir() - any way to limit # of file entries?

P

Peter A. Schott

I want to build a program that does some archiving. We have several programs
that have been dumping files into a folder (one folder) for some time now. I
want to limit the number of files returned by listdir to avoid trying to build a
list with tons of entries. I then want to move those files into a zip archive
for the appropriate day/month.

Is there any way to build a list of the first 1000 files or so in a folder?

Running Python 2.4.2 on Win32 with Win32 extensions.

Thanks.

-Pete Schott
 
P

Paul Rubin

Peter A. Schott said:
Is there any way to build a list of the first 1000 files or so in a folder?

The obvious way is

first_1000 = os.listdir()[:1000]

That does build a potentially bigger list in memory before chopping
off the 1000 elements, but unless you're running out of memory to hold
the list (which these days probably means millions of files in the
directory), don't worry about it.
 
L

Larry Bates

Peter said:
I want to build a program that does some archiving. We have several programs
that have been dumping files into a folder (one folder) for some time now. I
want to limit the number of files returned by listdir to avoid trying to build a
list with tons of entries. I then want to move those files into a zip archive
for the appropriate day/month.

Is there any way to build a list of the first 1000 files or so in a folder?

Running Python 2.4.2 on Win32 with Win32 extensions.

Thanks.

-Pete Schott


Since I assume that all the files are mixed together, I would probably
do this as a 2 step process:

Step 1 - Use os.walk and iterate over all the files moving them (using
os.shutil) into subdirectories based on their day/month information
(not sure where this information comes from).

Step 2 - Use os.walk to iterate over each of these directories and add
all the files contained in each day/month subdirectory into a .zip archive.

-Larry Bates
 
R

Roger Upole

You can use win32file.FindFilesIterator to loop thru the files
without creating a huge list of everthing in the folder.

hth
Roger
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top