<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'list files in a directory' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'list files in a directory' posted on the 'Python' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2012 Programmers Heaven</copyright>
    <pubDate>Wed, 23 May 2012 23:32:49 -0700</pubDate>
    <lastBuildDate>Wed, 23 May 2012 23:32:49 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>list files in a directory</title>
      <link>http://www.programmersheaven.com/mb/python/191482/191482/list-files-in-a-directory/</link>
      <description>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. &lt;br /&gt;
&lt;br /&gt;
for example, i have 3 files: file-1.txt,file-2.txt and file-3.txt&lt;br /&gt;
how do i make it so that only file-3.txt is shown?&lt;br /&gt;
&lt;br /&gt;
i found that i could use dircache.listdir(path) but that just lists the files. hope my question is not too confusing.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/python/191482/191482/list-files-in-a-directory/</guid>
      <pubDate>Wed, 28 May 2003 08:16:55 -0700</pubDate>
      <category>Python</category>
    </item>
    <item>
      <title>Re: list files in a directory</title>
      <link>http://www.programmersheaven.com/mb/python/191482/191502/re-list-files-in-a-directory/#191502</link>
      <description>: 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. &lt;br /&gt;
: &lt;br /&gt;
: for example, i have 3 files: file-1.txt,file-2.txt and file-3.txt&lt;br /&gt;
: how do i make it so that only file-3.txt is shown?&lt;br /&gt;
: &lt;br /&gt;
: i found that i could use dircache.listdir(path) but that just lists the files. hope my question is not too confusing.&lt;br /&gt;
&lt;br /&gt;
That's a tough one.  I'll let you know if I find anything.  Please post here if you find something yourself.  Thanks!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;&lt;em&gt;&lt;span style="color: Blue;"&gt;&lt;span style="color: Red;"&gt;i&lt;/span&gt;nfidel&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/python/191482/191502/re-list-files-in-a-directory/#191502</guid>
      <pubDate>Wed, 28 May 2003 10:01:34 -0700</pubDate>
      <category>Python</category>
    </item>
    <item>
      <title>Re: list files in a directory</title>
      <link>http://www.programmersheaven.com/mb/python/191482/191522/re-list-files-in-a-directory/#191522</link>
      <description>&lt;strong&gt;&lt;span style="color: Red;"&gt;This message was edited by rul at  2003-5-28 11:8:38&lt;/span&gt;&lt;/strong&gt;&lt;hr /&gt;&lt;br /&gt;
ok i got it partly working, here's the code&lt;br /&gt;
&lt;br /&gt;
import dircache&lt;br /&gt;
list = dircache.listdir('test')&lt;br /&gt;
i = 0&lt;br /&gt;
check = len(list[0])&lt;br /&gt;
temp = []&lt;br /&gt;
count = len(list)&lt;br /&gt;
&lt;br /&gt;
while count != 0:&lt;br /&gt;
    if len(list[i]) != check:&lt;br /&gt;
        temp.append(list[i-1])&lt;br /&gt;
        check = len(list[i])&lt;br /&gt;
    else:&lt;br /&gt;
        i = i + 1&lt;br /&gt;
    count = count - 1&lt;br /&gt;
&lt;br /&gt;
print temp&lt;br /&gt;
&lt;br /&gt;
------------------------------------------------------------------&lt;br /&gt;
inside the test directory, i have these files:&lt;br /&gt;
abc - 10.txt&lt;br /&gt;
abc - 11.txt&lt;br /&gt;
abc - 12.txt&lt;br /&gt;
xyz - 201.txt&lt;br /&gt;
xyz - 202.txt&lt;br /&gt;
xyz - 203.txt&lt;br /&gt;
&lt;br /&gt;
however, the output is only abc - 12.txt&lt;br /&gt;
what i want is for the program to output abc - 12.txt and xyz - 203.txt&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/python/191482/191522/re-list-files-in-a-directory/#191522</guid>
      <pubDate>Wed, 28 May 2003 11:07:32 -0700</pubDate>
      <category>Python</category>
    </item>
    <item>
      <title>I got it</title>
      <link>http://www.programmersheaven.com/mb/python/191482/191546/i-got-it/#191546</link>
      <description>Very simple once I stopped to actually think about it.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
import os

highestnumbers = {}
highestfiles = {}

for filename in os.listdir("C:\\TEMP\\py"):
    basename, extension = filename.split('.')
    prefix, number = basename.split(' - ')
    if number &amp;gt; 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])
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;&lt;em&gt;&lt;span style="color: Blue;"&gt;&lt;span style="color: Red;"&gt;i&lt;/span&gt;nfidel&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/python/191482/191546/i-got-it/#191546</guid>
      <pubDate>Wed, 28 May 2003 13:26:33 -0700</pubDate>
      <category>Python</category>
    </item>
    <item>
      <title>Re: I got it</title>
      <link>http://www.programmersheaven.com/mb/python/191482/191600/re-i-got-it/#191600</link>
      <description>: Very simple once I stopped to actually think about it.&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: import os
: 
: highestnumbers = {}
: highestfiles = {}
: 
: for filename in os.listdir("C:\\TEMP\\py"):
:     basename, extension = filename.split('.')
:     prefix, number = basename.split(' - ')
:     if number &amp;gt; 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])
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: 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.&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
: &lt;span style="font-size: large;"&gt;&lt;em&gt;&lt;span style="color: Blue;"&gt;&lt;span style="color: Red;"&gt;i&lt;/span&gt;nfidel&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
it works great! thanks a lot!&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/python/191482/191600/re-i-got-it/#191600</guid>
      <pubDate>Wed, 28 May 2003 21:02:00 -0700</pubDate>
      <category>Python</category>
    </item>
    <item>
      <title>This post has been deleted.</title>
      <link>http://www.programmersheaven.com/mb/python/191482/412609/this-post-has-been-deleted/#412609</link>
      <description>This post has been deleted.</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/python/191482/412609/this-post-has-been-deleted/#412609</guid>
      <pubDate>Sat, 30 Jan 2010 03:13:19 -0700</pubDate>
      <category>Python</category>
    </item>
    <item>
      <title>This post has been deleted.</title>
      <link>http://www.programmersheaven.com/mb/python/191482/412610/this-post-has-been-deleted/#412610</link>
      <description>This post has been deleted.</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/python/191482/412610/this-post-has-been-deleted/#412610</guid>
      <pubDate>Sat, 30 Jan 2010 03:16:36 -0700</pubDate>
      <category>Python</category>
    </item>
  </channel>
</rss>
