list files in a directory

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.

Comments

  • : 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!


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • [b][red]This message was edited by rul at 2003-5-28 11:8:38[/red][/b][hr]
    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


  • Very simple once I stopped to actually think about it.

    [code]
    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])
    [/code]

    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.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • : Very simple once I stopped to actually think about it.
    :
    : [code]
    : 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])
    : [/code]
    :
    : 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.
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    :

    it works great! thanks a lot!
  • This post has been deleted.
  • This post has been deleted.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories