<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
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/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Autarchy of the Private Cave &#187; glob</title> <atom:link href="https://bogdan.org.ua/tags/glob/feed" rel="self" type="application/rss+xml" /><link>https://bogdan.org.ua</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Wed, 28 Dec 2022 16:09:04 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>https://wordpress.org/?v=3.8.27</generator> <item><title>Python: iterate (and read) all files in a directory (folder)</title><link>https://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html</link> <comments>https://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html#comments</comments> <pubDate>Sun, 12 Aug 2007 11:33:57 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[directory]]></category> <category><![CDATA[files]]></category> <category><![CDATA[folder]]></category> <category><![CDATA[glob]]></category> <category><![CDATA[iterate]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html</guid> <description><![CDATA[To iterate through all the files within the specified directory (folder), with ability to use wildcards (*, ?, and [ ]-style ranges), use the following code snippet: import os import glob &#160; path = 'sequences/' for infile in glob.glob&#40; os.path.join&#40;path, '*.fasta'&#41; &#41;: &#160; &#160; print &#34;current file is: &#34; + infile If you do not [&#8230;]]]></description> <content:encoded><![CDATA[<p>To iterate through all the files within the specified directory (folder), with ability to use wildcards (*, ?, and [ ]-style ranges), use the following code snippet:<br
/> <span
id="more-200"></span></p><div
id="ig-sh-1" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">os</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">glob</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">path <span
style="color: #66cc66;">=</span> <span
style="color: #483d8b;">'sequences/'</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> infile <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #dc143c;">glob</span>.<span
style="color: #dc143c;">glob</span><span
style="color: black;">&#40;</span> <span
style="color: #dc143c;">os</span>.<span
style="color: black;">path</span>.<span
style="color: black;">join</span><span
style="color: black;">&#40;</span>path<span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'*.fasta'</span><span
style="color: black;">&#41;</span> <span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">&quot;current file is: &quot;</span> + infile</div></li></ol></div></div><p>If you do not need wildcards, then there is a simpler way to list all items in a directory:</p><div
id="ig-sh-2" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">os</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">path <span
style="color: #66cc66;">=</span> <span
style="color: #483d8b;">'sequences/'</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">listing <span
style="color: #66cc66;">=</span> <span
style="color: #dc143c;">os</span>.<span
style="color: black;">listdir</span><span
style="color: black;">&#40;</span>path<span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> infile <span
style="color: #ff7700;font-weight:bold;">in</span> listing:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">&quot;current file is: &quot;</span> + infile</div></li></ol></div></div><p><strong>print</strong> was promoted from a statement to a <a
href="http://www.harshj.com/2008/12/09/the-new-print-function-in-python-3/">function</a> in Python 3 (use <strong>print(infile)</strong> instead of <strong>print infile</strong>).</p><p>One should use &#8216;os.path.join()&#8217; part to make the script cross-platform-portable (different OS use different path separators, and hard-coding path separator would stop the script from executing under a different OS).</p><p><a
href="http://docs.python.org/library/glob.html">Python docs</a> mention that there is also iglob(), which is an iterator and thus working on directories with way too many files it will save memory by returning only single result per iteration, and not the whole list of files &#8211; as glob() does.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fpython-iterate-and-read-all-files-in-a-directory-folder.html&amp;linkname=Python%3A%20iterate%20%28and%20read%29%20all%20files%20in%20a%20directory%20%28folder%29" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fpython-iterate-and-read-all-files-in-a-directory-folder.html&amp;linkname=Python%3A%20iterate%20%28and%20read%29%20all%20files%20in%20a%20directory%20%28folder%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fpython-iterate-and-read-all-files-in-a-directory-folder.html&amp;linkname=Python%3A%20iterate%20%28and%20read%29%20all%20files%20in%20a%20directory%20%28folder%29" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fpython-iterate-and-read-all-files-in-a-directory-folder.html&amp;linkname=Python%3A%20iterate%20%28and%20read%29%20all%20files%20in%20a%20directory%20%28folder%29" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fpython-iterate-and-read-all-files-in-a-directory-folder.html&amp;linkname=Python%3A%20iterate%20%28and%20read%29%20all%20files%20in%20a%20directory%20%28folder%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2007%2F08%2F12%2Fpython-iterate-and-read-all-files-in-a-directory-folder.html&#038;title=Python%3A%20iterate%20%28and%20read%29%20all%20files%20in%20a%20directory%20%28folder%29" data-a2a-url="https://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html" data-a2a-title="Python: iterate (and read) all files in a directory (folder)"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2007/08/12/python-iterate-and-read-all-files-in-a-directory-folder.html/feed</wfw:commentRss> <slash:comments>39</slash:comments> </item> </channel> </rss>