{"id":4209,"date":"2022-08-06T01:32:33","date_gmt":"2022-08-06T01:32:33","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4209"},"modified":"2025-03-30T13:02:52","modified_gmt":"2025-03-30T13:02:52","slug":"python-list-files","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-list-files\/","title":{"rendered":"How to List Files from a Directory in Python"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to list files in a directory using the Python <code>os.walk()<\/code> function.<\/p>\n\n\n\n<p>Sometimes, you may want to list all files from a directory for processing. For example, you might want to find all images of a directory and resize each of them. To list all files in a directory, you can use the <code>os.walk()<\/code> function.<\/p>\n\n\n\n<p>The <code>os.<code>walk()<\/code><\/code> function generates file names in a directory by walking the tree either top-down or bottom-up. The <code>os.walk()<\/code> function yields a tuple with three fields (<code>dirpath<\/code>, <code>dirnames<\/code>, and <code>filenames<\/code>) for each directory in the directory tree.<\/p>\n\n\n\n<p class=\"note\">Note that the <code>os.walk()<\/code> function examines the whole directory tree. Therefore, you can use it to get all files from all directories and their subdirectories of a root directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-list-file-example'>Python list file example <a href=\"#python-list-file-example\" class=\"anchor\" id=\"python-list-file-example\" title=\"Anchor for Python list file example\">#<\/a><\/h2>\n\n\n\n<p>Suppose you have a folder <code>D:\\web<\/code> with the following directories and files:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">D:\\web\n\u251c\u2500\u2500 assets\n|  \u251c\u2500\u2500 css\n|  |  \u2514\u2500\u2500 style.css\n|  \u2514\u2500\u2500 js\n|     \u2514\u2500\u2500 app.js\n\u251c\u2500\u2500 blog\n|  \u251c\u2500\u2500 read-file.html\n|  \u2514\u2500\u2500 write-file.html\n\u251c\u2500\u2500 about.html\n\u251c\u2500\u2500 contact.html\n\u2514\u2500\u2500 index.html<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p id=\"index-30\">The following example shows how to use the <code>os.walk()<\/code> function to list all HTML files from the <code>D:\\web<\/code> directory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> os\n\n\npath = <span class=\"hljs-string\">'D:\\\\web'<\/span>\n\nhtml_files = &#91;]\n\n<span class=\"hljs-keyword\">for<\/span> dirpath, dirnames, filenames <span class=\"hljs-keyword\">in<\/span> os.walk(path):\n    <span class=\"hljs-keyword\">for<\/span> filename <span class=\"hljs-keyword\">in<\/span> filenames:\n        <span class=\"hljs-keyword\">if<\/span> filename.endswith(<span class=\"hljs-string\">'.html'<\/span>):\n            html_files.append(os.path.join(dirpath, filename))\n\n<span class=\"hljs-keyword\">for<\/span> html_file <span class=\"hljs-keyword\">in<\/span> html_files:\n    print(html_file)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">D:\\web\\about.html\nD:\\web\\contact.html\nD:\\web\\index.html\nD:\\web\\blog\\read-file.html\nD:\\web\\blog\\write-file.html<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, initialize a list to store the path to HTML files:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">html_files = &#91;]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, call <code>os.walk()<\/code> function to examine directories of the <code>D:\\web<\/code> folder:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">for<\/span> dirpath, dirnames, filenames <span class=\"hljs-keyword\">in<\/span> os.walk(path):<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>dirpath<\/code> stores the directory and filenames store files in that directory.<\/p>\n\n\n\n<p>Third, loop over the filenames and add them to the <code>html_files<\/code> list if their extensions are <code>.html<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># ...<\/span>\n<span class=\"hljs-keyword\">for<\/span> filename <span class=\"hljs-keyword\">in<\/span> filenames:\n        <span class=\"hljs-keyword\">if<\/span> filename.endswith(<span class=\"hljs-string\">'.html'<\/span>):\n            html_files.append(os.path.join(dirpath, filename))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the <code>os.path.join()<\/code> returns the full path of the filename by joining the <code>dirpath<\/code> with the <code>filename<\/code>.<\/p>\n\n\n\n<p>Finally, print output the filenames in the <code>html_files<\/code> list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">for<\/span> html_file <span class=\"hljs-keyword\">in<\/span> html_files:\n    print(html_file)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='defining-a-reusable-list-files-function'>Defining a reusable list files function <a href=\"#defining-a-reusable-list-files-function\" class=\"anchor\" id=\"defining-a-reusable-list-files-function\" title=\"Anchor for Defining a reusable list files function\">#<\/a><\/h2>\n\n\n\n<p>By using the <code>os.walk()<\/code> function, we can define a reusable <code>list_files()<\/code> function like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> os\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">list_files<\/span><span class=\"hljs-params\">(path, extentions=None)<\/span>:<\/span>\n    <span class=\"hljs-string\">\"\"\" List all files in a directory specified by path\n    Args:\n        path - the root directory path\n        extensions - a iterator of file extensions to include, pass None to get all files.\n    Returns:\n        A list of files specified by extensions\n    \"\"\"<\/span>\n    filepaths = &#91;]\n    <span class=\"hljs-keyword\">for<\/span> root, _, files <span class=\"hljs-keyword\">in<\/span> os.walk(path):\n        <span class=\"hljs-keyword\">for<\/span> file <span class=\"hljs-keyword\">in<\/span> files:\n            <span class=\"hljs-keyword\">if<\/span> extentions <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-literal\">None<\/span>:\n                filepaths.append(os.path.join(root, file))\n            <span class=\"hljs-keyword\">else<\/span>:\n                <span class=\"hljs-keyword\">for<\/span> ext <span class=\"hljs-keyword\">in<\/span> extentions:\n                    <span class=\"hljs-keyword\">if<\/span> file.endswith(ext):\n                        filepaths.append(os.path.join(root, file))\n\n    <span class=\"hljs-keyword\">return<\/span> filepaths\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    filepaths = list_files(<span class=\"hljs-string\">r'D:\\web'<\/span>, (<span class=\"hljs-string\">'.html'<\/span>, <span class=\"hljs-string\">'.css'<\/span>))\n    <span class=\"hljs-keyword\">for<\/span> filepath <span class=\"hljs-keyword\">in<\/span> filepaths:\n        print(filepath)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">D:\\web\\about.html\nD:\\web\\contact.html\nD:\\web\\index.html\nD:\\web\\assets\\css\\style.css\nD:\\web\\blog\\read-file.html\nD:\\web\\blog\\write-file.html<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='make-list-files-function-more-efficient'>Make list files function more efficient <a href=\"#make-list-files-function-more-efficient\" class=\"anchor\" id=\"make-list-files-function-more-efficient\" title=\"Anchor for Make list files function more efficient\">#<\/a><\/h2>\n\n\n\n<p>If the number of files is small, the <code>list_files()<\/code> function works fine. However, when the number of files is large, returning a large list of files is not memory efficient. <\/p>\n\n\n\n<p>To resolve this, you can use a <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-generators\/\">generator<\/a> to yield each file at a time instead of returning a list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> os\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">list_files<\/span><span class=\"hljs-params\">(path, extentions=None)<\/span>:<\/span>\n    <span class=\"hljs-string\">\"\"\" List all files in a directory specified by path\n    Args:\n        path - the root directory path\n        extensions - a iterator of file extensions to include, pass None to get all files.\n    Returns:\n        A list of files specified by extensions\n    \"\"\"<\/span>\n    <span class=\"hljs-keyword\">for<\/span> root, _, files <span class=\"hljs-keyword\">in<\/span> os.walk(path):\n        <span class=\"hljs-keyword\">for<\/span> file <span class=\"hljs-keyword\">in<\/span> files:\n            <span class=\"hljs-keyword\">if<\/span> extentions <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-literal\">None<\/span>:\n                <span class=\"hljs-keyword\">yield<\/span> os.path.join(root, file)\n            <span class=\"hljs-keyword\">else<\/span>:\n                <span class=\"hljs-keyword\">for<\/span> ext <span class=\"hljs-keyword\">in<\/span> extentions:\n                    <span class=\"hljs-keyword\">if<\/span> file.endswith(ext):\n                        <span class=\"hljs-keyword\">yield<\/span> os.path.join(root, file)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    filepaths = list_files(<span class=\"hljs-string\">r'D:\\web'<\/span>, (<span class=\"hljs-string\">'.html'<\/span>, <span class=\"hljs-string\">'.css'<\/span>))\n    <span class=\"hljs-keyword\">for<\/span> filepath <span class=\"hljs-keyword\">in<\/span> filepaths:\n        print(filepath)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>os.walk()<\/code> function to list files in a directory recursively.<\/li>\n\n\n\n<li>Define a reusable function for listing files in a directory using the <code>os.walk()<\/code> function.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=list-files\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"4209\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list-files\/\"\n\t\t\t\tdata-post-title=\"How to List Files from a Directory in Python\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"4209\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list-files\/\"\n\t\t\t\tdata-post-title=\"How to List Files from a Directory in Python\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to list files in a directory using the Python os.walk() function. Sometimes, you may want to list all files from a directory for processing. For example, you might want to find all images of a directory and resize each of them. To list all files in a directory, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":74,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4209","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=4209"}],"version-history":[{"count":2,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4209\/revisions"}],"predecessor-version":[{"id":7242,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4209\/revisions\/7242"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=4209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}