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

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

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
Feb 22 '06 #1
3 5585
Peter A. Schott <pa******@no.yahoo.spamm.com> writes:
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.
Feb 22 '06 #2
Peter A. Schott wrote:
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
Feb 22 '06 #3
You can use win32file.FindFilesIterator to loop thru the files
without creating a huge list of everthing in the folder.

hth
Roger

"Peter A. Schott" <pa******@no.yahoo.spamm.com> wrote in message news:bu********************************@4ax.com...
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


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 22 '06 #4

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

Similar topics

5
by: James Butler | last post by:
Running a CLI script that fopen's a file, parses the lines into an array, checks the array entries against a few regular expression qualifiers (i.e. !eregi("bot",$entry)) and dump the good entries...
11
by: Jason Kratz | last post by:
OK. I've search on google groups and around the web for this and I haven't found an answer. I'm a Python newbie and have what I assume is a basic question. os.listdir takes a pathname as an arg...
8
by: Hannu Kankaanp?? | last post by:
This may be a bug or simply a strange result of undefined behaviour, but this is what I get with Python 2.3.2 on Windows XP: >>> import os >>> os.listdir('') >>> os.listdir(u'')
7
by: Kenneth Pronovici | last post by:
I have some confusion regarding the relationship between locale, os.listdir() and unicode pathnames. I'm running Python 2.3.5 on a Debian system. If it matters, all of the files I'm dealing with...
15
by: Riccardo Galli | last post by:
Hi, I noticed that when I use os.listdir I need to work with absolute paths 90% of times. While I can use a for cycle, I'd prefere to use a list comprehension, but it becomes too long. I...
6
by: Hannu | last post by:
Hi. In the ldb file you can see the users of the mdb-file. If you open the mdb-file your machine and username will be written in the lbd- file. Allthough you close the mdb-file your name won't...
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
27
by: ted benedict | last post by:
hi everybody, i hope this is the right place to discuss this weird behaviour. i am getting dynamically generated text or xml from the server side using xmlhttprequest. if the server side data is...
0
by: Mark | last post by:
Thanks in Advance. I encountered the 4 MB limit recently while using the export feature of Crystal Reports version 9 Report Application Server which uses a MS SQL Server 2000 as a data source. ...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.