list files in a directory - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

list files in a directory

rulrul Posts: 8Member
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

  • infidelinfidel Posts: 2,900Member
    : 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]

  • rulrul Posts: 8Member
    [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


  • infidelinfidel Posts: 2,900Member
    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]

  • rulrul Posts: 8Member
    : 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!
  • Shumaker212Shumaker212 Posts: 4Member
    This post has been deleted.
  • Shumaker212Shumaker212 Posts: 4Member
    This post has been deleted.
Sign In or Register to comment.