Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
list files in a directory Posted by rul on 28 May 2003 at 8:16 AM
hi, i just started with python. i want to get a list of files in a directory but i only want to see the latest files. i need this so that i can see what's the latest file i have based on their number.

for example, i have 3 files: file-1.txt,file-2.txt and file-3.txt
how do i make it so that only file-3.txt is shown?

i found that i could use dircache.listdir(path) but that just lists the files. hope my question is not too confusing.
Report
Re: list files in a directory Posted by infidel on 28 May 2003 at 10:01 AM
: hi, i just started with python. i want to get a list of files in a directory but i only want to see the latest files. i need this so that i can see what's the latest file i have based on their number.
:
: for example, i have 3 files: file-1.txt,file-2.txt and file-3.txt
: how do i make it so that only file-3.txt is shown?
:
: i found that i could use dircache.listdir(path) but that just lists the files. hope my question is not too confusing.

That's a tough one. I'll let you know if I find anything. Please post here if you find something yourself. Thanks!


infidel

Report
Re: list files in a directory Posted by rul on 28 May 2003 at 11:07 AM
This message was edited by rul at 2003-5-28 11:8:38

ok i got it partly working, here's the code

import dircache
list = dircache.listdir('test')
i = 0
check = len(list[0])
temp = []
count = len(list)

while count != 0:
if len(list[i]) != check:
temp.append(list[i-1])
check = len(list[i])
else:
i = i + 1
count = count - 1

print temp

------------------------------------------------------------------
inside the test directory, i have these files:
abc - 10.txt
abc - 11.txt
abc - 12.txt
xyz - 201.txt
xyz - 202.txt
xyz - 203.txt

however, the output is only abc - 12.txt
what i want is for the program to output abc - 12.txt and xyz - 203.txt


Report
I got it Posted by infidel on 28 May 2003 at 1:26 PM
Very simple once I stopped to actually think about it.

import os

highestnumbers = {}
highestfiles = {}

for filename in os.listdir("C:\\TEMP\\py"):
    basename, extension = filename.split('.')
    prefix, number = basename.split(' - ')
    if number > highestnumbers.get(prefix, 0):
        highestnumbers[prefix] = number
        highestfiles[prefix] = filename

prefixes = highestfiles.keys()
prefixes.sort()
for prefix in prefixes:
    print 'Latest file for %s prefix is: %s' % (prefix, highestfiles[prefix])


This, of course, depends on consistently applied naming conventions. If the file names change patterns at all then it won't work. Should get you started, though.


infidel

Report
Re: I got it Posted by rul on 28 May 2003 at 9:02 PM
: Very simple once I stopped to actually think about it.
:
:
: import os
: 
: highestnumbers = {}
: highestfiles = {}
: 
: for filename in os.listdir("C:\\TEMP\\py"):
:     basename, extension = filename.split('.')
:     prefix, number = basename.split(' - ')
:     if number > highestnumbers.get(prefix, 0):
:         highestnumbers[prefix] = number
:         highestfiles[prefix] = filename
: 
: prefixes = highestfiles.keys()
: prefixes.sort()
: for prefix in prefixes:
:     print 'Latest file for %s prefix is: %s' % (prefix, highestfiles[prefix])
: 

:
: This, of course, depends on consistently applied naming conventions. If the file names change patterns at all then it won't work. Should get you started, though.
:
:
: infidel
:
:

it works great! thanks a lot!
Report
This post has been deleted. Posted by Shumaker212 on 30 Jan 2010 at 3:13 AM
This post has been deleted.
Report
This post has been deleted. Posted by Shumaker212 on 30 Jan 2010 at 3:16 AM
This post has been deleted.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.