<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Python: iterate (and read) all files in a directory (folder)</title> <atom:link href="http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html/feed" rel="self" type="application/rss+xml" /><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Wed, 08 Feb 2012 09:31:46 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-136417</link> <dc:creator>born</dc:creator> <pubDate>Thu, 13 Oct 2011 12:40:26 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-136417</guid> <description>hi ...
I have been messing around with a python program to browse through images in a directory and display it in a canvas.can anybody help??</description> <content:encoded><![CDATA[<p>hi &#8230;<br
/> I have been messing around with a python program to browse through images in a directory and display it in a canvas.can anybody help??</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-136150</link> <dc:creator>toto</dc:creator> <pubDate>Wed, 14 Sep 2011 09:28:56 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-136150</guid> <description>thank you very much for your explaining. I get a problem when try to list file or directory in Python. You solve my problem :)</description> <content:encoded><![CDATA[<p>thank you very much for your explaining. I get a problem when try to list file or directory in Python. You solve my problem <img
src='http://bogdan.org.ua/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-135820</link> <dc:creator>ablaze</dc:creator> <pubDate>Wed, 10 Aug 2011 12:54:24 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-135820</guid> <description>Hi...
I am working in ubuntu. I have a bunch of commands (say 10 commands like cmd1, cmd2, cmd3..............cmd10)
I want to write a python script, which can achive the following:
It should traverse through the directory structure and apply a command at particular directory path.
The location and the commands are already known to me.
/local/mnt/myspace/sample1$ cmd1
/local/mnt/myspace/sample2$ cmd2
/local/mnt/myspace$ cmd3
/local/mnt$cmd4
/local/mnt/myspace/sample9$ cmd 8
/local/mnt/myspace/sample3$ dmd10
can someone please provide the script as I am not event a beginner in python.</description> <content:encoded><![CDATA[<p>Hi&#8230;<br
/> I am working in ubuntu. I have a bunch of commands (say 10 commands like cmd1, cmd2, cmd3&#8230;&#8230;&#8230;&#8230;..cmd10)<br
/> I want to write a python script, which can achive the following:</p><p>It should traverse through the directory structure and apply a command at particular directory path.<br
/> The location and the commands are already known to me.</p><p>/local/mnt/myspace/sample1$ cmd1<br
/> /local/mnt/myspace/sample2$ cmd2<br
/> /local/mnt/myspace$ cmd3<br
/> /local/mnt$cmd4<br
/> /local/mnt/myspace/sample9$ cmd 8<br
/> /local/mnt/myspace/sample3$ dmd10</p><p>can someone please provide the script as I am not event a beginner in python.</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-135000</link> <dc:creator>Stefan</dc:creator> <pubDate>Fri, 13 May 2011 12:20:47 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-135000</guid> <description>I found it:
dirList=os.listdir(path)
dirList.sort()
for fname in dirList:
print( fname)</description> <content:encoded><![CDATA[<p>I found it:</p><p>dirList=os.listdir(path)</p><p>dirList.sort()</p><p>for fname in dirList:<br
/> print( fname)</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-134999</link> <dc:creator>Stefan</dc:creator> <pubDate>Fri, 13 May 2011 11:51:45 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-134999</guid> <description>Is there a possibility to list the files in order, by name ?
For example :
/path/file01.txt
/path/file02.txt
..............
If I use the codes you presented here i get scrambled order</description> <content:encoded><![CDATA[<p>Is there a possibility to list the files in order, by name ?<br
/> For example :<br
/> /path/file01.txt<br
/> /path/file02.txt<br
/> &#8230;&#8230;&#8230;&#8230;..</p><p>If I use the codes you presented here i get scrambled order</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-133550</link> <dc:creator>Rommel</dc:creator> <pubDate>Wed, 26 Jan 2011 19:05:49 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-133550</guid> <description>Thanks. This snippet helped a bunch.</description> <content:encoded><![CDATA[<p>Thanks. This snippet helped a bunch.</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-122575</link> <dc:creator>Bill Tate</dc:creator> <pubDate>Mon, 29 Nov 2010 23:47:43 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-122575</guid> <description>Is there a way to also do thiw in Windows? What I need to do is
process every *.txt file in a directory, one at a time, inside
a Python script.</description> <content:encoded><![CDATA[<p>Is there a way to also do thiw in Windows? What I need to do is<br
/> process every *.txt file in a directory, one at a time, inside<br
/> a Python script.</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-103407</link> <dc:creator>Dan</dc:creator> <pubDate>Tue, 12 Jan 2010 02:43:47 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-103407</guid> <description>Ahh... My *.* as opposed to a * had it so it wasn&#039;t looking at folders, thus the problem. Thanks again!</description> <content:encoded><![CDATA[<p>Ahh&#8230; My *.* as opposed to a * had it so it wasn&#8217;t looking at folders, thus the problem. Thanks again!</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-103404</link> <dc:creator>Bogdan</dc:creator> <pubDate>Mon, 11 Jan 2010 21:44:16 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-103404</guid> <description>Dan,
script below seems to work perfectly for me:
&lt;pre&gt;&lt;code&gt;
import os, glob
def scandirs(path):
for currentFile in glob.glob( os.path.join(path, &#039;*&#039;) ):
if os.path.isdir(currentFile):
print &#039;got a directory: &#039; + currentFile
scandirs(currentFile)
print &quot;processing file: &quot; + currentFile
scandirs(&#039;Desktop&#039;)
&lt;/code&gt;&lt;/pre&gt;
Basically, I&#039;ve changed the &#039;*.*&#039; wildcard to just &#039;*&#039;.</description> <content:encoded><![CDATA[<p>Dan,</p><p>script below seems to work perfectly for me:</p><pre><code>
import os, glob
def scandirs(path):
    for currentFile in glob.glob( os.path.join(path, '*') ):
        if os.path.isdir(currentFile):
            print 'got a directory: ' + currentFile
            scandirs(currentFile)
        print "processing file: " + currentFile
scandirs('Desktop')
</code></pre><p>Basically, I&#8217;ve changed the &#8216;*.*&#8217; wildcard to just &#8216;*&#8217;.</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-103402</link> <dc:creator>Dan</dc:creator> <pubDate>Mon, 11 Jan 2010 20:07:09 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-103402</guid> <description>Bogdan,
Thanks for the help. I&#039;m still not getting the code to look at the directories within the path. Here&#039;s my code, it still only looks at the files under the initial path.
&lt;pre&gt;&lt;code&gt;
def scandirs(path):
for currentFile in glob.glob( os.path.join(path, &#039;*.*&#039;) ):
if os.path.isdir(currentFile):
scandirs(currentFile)
print &quot;processing file: &quot; + currentFile
scandirs(&#039;XML/&#039;)
&lt;/code&gt;&lt;/pre&gt;</description> <content:encoded><![CDATA[<p>Bogdan,</p><p>Thanks for the help. I&#8217;m still not getting the code to look at the directories within the path. Here&#8217;s my code, it still only looks at the files under the initial path.</p><pre><code>
def scandirs(path):
    for currentFile in glob.glob( os.path.join(path, '*.*') ):
        if os.path.isdir(currentFile):
            scandirs(currentFile)
        print "processing file: " + currentFile
scandirs('XML/')
</code></pre>]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-103321</link> <dc:creator>Bogdan</dc:creator> <pubDate>Sun, 03 Jan 2010 19:07:25 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-103321</guid> <description>Make that code into a function - e.g. scan_dirs(path) - and add a single line of code to it (pseudocode below):
&lt;code&gt;
if os.path.isdir(infile): scan_dirs(infile)
&lt;/code&gt;
This will do exactly what you want.</description> <content:encoded><![CDATA[<p>Make that code into a function &#8211; e.g. scan_dirs(path) &#8211; and add a single line of code to it (pseudocode below):<br
/> <code><br
/> if os.path.isdir(infile): scan_dirs(infile)<br
/> </code></p><p>This will do exactly what you want.</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-103320</link> <dc:creator>Dan</dc:creator> <pubDate>Sun, 03 Jan 2010 18:14:11 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-103320</guid> <description>Is there a way to change this script so that it also runs through sub-directories under the given path name?</description> <content:encoded><![CDATA[<p>Is there a way to change this script so that it also runs through sub-directories under the given path name?</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-102985</link> <dc:creator>Kris</dc:creator> <pubDate>Mon, 07 Dec 2009 00:11:09 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-102985</guid> <description>import os, glob
path = &#039;./&#039;
for infile in glob.glob( os.path.join(path, &#039;*.*&#039;) ):
print(&quot;current file is: &quot; + infile)
#lists all files in directory script is in</description> <content:encoded><![CDATA[<p>import os, glob<br
/> path = &#8216;./&#8217;<br
/> for infile in glob.glob( os.path.join(path, &#8216;*.*&#8217;) ):<br
/> print(&#8220;current file is: &#8221; + infile)</p><p>#lists all files in directory script is in</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-102846</link> <dc:creator>Richard</dc:creator> <pubDate>Tue, 24 Nov 2009 01:38:22 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-102846</guid> <description>marvellous</description> <content:encoded><![CDATA[<p>marvellous</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-101297</link> <dc:creator>Ferralll</dc:creator> <pubDate>Mon, 18 May 2009 20:25:06 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-101297</guid> <description>Thankyou very much...
This was exactly what I was looking for!</description> <content:encoded><![CDATA[<p>Thankyou very much&#8230;<br
/> This was exactly what I was looking for!</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-101256</link> <dc:creator>Mike</dc:creator> <pubDate>Thu, 14 May 2009 01:52:37 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-101256</guid> <description>import os, glob
def dir(path):
for infile in glob.glob( os.path.join(path) ):
print &quot;current file is: &quot; + infile
path = dir(raw_input(&quot;Enter the path: &quot;))</description> <content:encoded><![CDATA[<p>import os, glob</p><p>def dir(path):<br
/> for infile in glob.glob( os.path.join(path) ):<br
/> print &#8220;current file is: &#8221; + infile</p><p>path = dir(raw_input(&#8220;Enter the path: &#8220;))</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-94523</link> <dc:creator>Bogdan</dc:creator> <pubDate>Tue, 23 Dec 2008 11:21:08 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-94523</guid> <description>Dt, thanks, I&#039;ve updated the code.</description> <content:encoded><![CDATA[<p>Dt, thanks, I&#8217;ve updated the code.</p> ]]></content:encoded> </item> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-94508</link> <dc:creator>Dt</dc:creator> <pubDate>Tue, 23 Dec 2008 09:38:45 +0000</pubDate> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comment-94508</guid> <description>works just fine for me, only important change to the code that i had to make was turning print into a function because im using python 3.0, i also set it to read files with *all* extensions.
import os, glob
path = &#039;insert your own path you lazy bastards &#039;
for infile in glob.glob( os.path.join(path, &#039;*.*&#039;) ):
print(&quot;current file is: &quot; + infile)</description> <content:encoded><![CDATA[<p>works just fine for me, only important change to the code that i had to make was turning print into a function because im using python 3.0, i also set it to read files with *all* extensions.</p><p>import os, glob</p><p>path = &#8216;insert your own path you lazy bastards &#8216;</p><p>for infile in glob.glob( os.path.join(path, &#8216;*.*&#8217;) ):<br
/> print(&#8220;current file is: &#8221; + infile)</p> ]]></content:encoded> </item> </channel> </rss>
