{"id":7707,"date":"2021-01-26T15:42:58","date_gmt":"2021-01-26T10:12:58","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=7707"},"modified":"2023-05-07T14:20:39","modified_gmt":"2023-05-07T08:50:39","slug":"python-listdir","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-listdir\/","title":{"rendered":"8 Examples to Implement os.listdir() in Python"},"content":{"rendered":"\n<p>Hello coders!! This article will be learning about the <strong>os.listdir<\/strong> function in Python.<strong> The os module allows us to use the functionality dependent on the operating system in a portable manner. listdir() is a method available in the os module. It returns a list containing the contents of the directory given by path<\/strong>. Let us learn about this topic in detail.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-listdir\/#What_is_the_oslistdir_method_in_Python\" >What is the os.listdir() method in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-listdir\/#Example_1_To_get_the_files_and_directories_in_root_directory_using_listdir\" >Example 1: To get the files and directories in root directory using listdir():<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-listdir\/#Example_2_To_get_the_files_and_directories_in_present_working_directory\" >Example 2: To get the files and directories in present working directory:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-listdir\/#Example_3_Not_using_the_path_parameter_in_listdir_In_Python\" >Example 3: Not using the path parameter in listdir() In Python:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-listdir\/#Example_4_loop_listdir_in_try-except_using_Python\" >Example 4: loop listdir in try-except using Python:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-listdir\/#Example_5_Python_listdir_sort_by_date\" >Example 5: Python listdir sort by date:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-listdir\/#Example_6_Python_listdir_sort_by_name\" >Example 6: Python listdir sort by name:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-listdir\/#Example_7_Python_listdir_absolute_path\" >Example 7: Python listdir absolute path:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-listdir\/#Example_8_Python_listdir_with_specific_extension\" >Example 8: Python listdir with specific extension:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-listdir\/#Conclusion_Python_oslistdir\" >Conclusion | Python os.listdir():<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-the-os-listdir-method-in-python\"><span class=\"ez-toc-section\" id=\"What_is_the_oslistdir_method_in_Python\"><\/span>What is the os.listdir() method in Python?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It is a method available in the os module of Python. This method is used to retrieve the list of files and directories present in the specified directory. In case the directory is not specified, the contents of the present working directory are returned.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>SYNATX:<\/strong> os.listdir(path)<\/li>\n\n\n\n<li><strong>PARAMETERS:<\/strong> It is optional. It contains the path<em> <\/em>of the directory.<\/li>\n\n\n\n<li><strong>RETURN VALUE: a list containing the entries&#8217; names<\/strong> in the directory given by path.<\/li>\n<\/ul>\n\n\n\n<p><strong>Let us see some illustrated examples to use listdir() using Python.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-example-1-to-get-the-files-and-directories-in-root-directory-using-listdir\"><span class=\"ez-toc-section\" id=\"Example_1_To_get_the_files_and_directories_in_root_directory_using_listdir\"><\/span>Example 1: To get the files and directories in root directory using listdir():<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os \npath = &quot;\/&quot;\ndirct = os.listdir(path) \nprint(&quot;Files and directories:&quot;) \nprint(dirct)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-output-explanation\">Output &amp; Explanation:<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-55.png\" alt=\"Output of listdir() to get content of root\" class=\"wp-image-7734\" width=\"802\" height=\"61\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-55.png 802w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-55-300x23.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-55-768x58.png 768w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><figcaption class=\"wp-element-caption\"><strong><em>Output<\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<p>In this example, we selected the root directory as our desired path. We then used the listdir() method to get the list of directories and files present inside the root directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-example-2-to-get-the-files-and-directories-in-present-working-directory\"><span class=\"ez-toc-section\" id=\"Example_2_To_get_the_files_and_directories_in_present_working_directory\"><\/span>Example 2: To get the files and directories in present working directory:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os \npath = os.getcwd() \ndirct = os.listdir(path) \nprint(&quot;Path:&quot;)\nprint(path)\nprint()\nprint(&quot;Files and directories:&quot;) \nprint(dirct) \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-output-explanation-1\">Output &amp; Explanation:<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-56.png\" alt=\"Output of listdir() to get content of present working directory\" class=\"wp-image-7736\" width=\"824\" height=\"246\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-56.png 824w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-56-300x90.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-56-768x229.png 768w\" sizes=\"(max-width: 824px) 100vw, 824px\" \/><figcaption class=\"wp-element-caption\"><strong><em>Output<\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<p>We first used the getcwd() method to get the path of the current working directory. We then passed this path as a parameter to the method to retrieve the contents of the current working directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-example-3-not-using-the-path-parameter-in-listdir-in-python\"><span class=\"ez-toc-section\" id=\"Example_3_Not_using_the_path_parameter_in_listdir_In_Python\"><\/span>Example 3: Not using the path parameter in listdir() In Python:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os \ndirct = os.listdir() \n\nprint(&quot;Files and directories:&quot;) \nprint(dirct) \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-output-explanation-2\">Output &amp; Explanation:<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-57.png\" alt=\"Output of listdir() without passing the parameter\" class=\"wp-image-7737\" width=\"824\" height=\"212\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-57.png 824w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-57-300x77.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-57-768x198.png 768w\" sizes=\"(max-width: 824px) 100vw, 824px\" \/><figcaption class=\"wp-element-caption\"><strong><em>Output<\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<p>In this particular example, we did not pass the path parameter of the function. As a result, the function automatically took the path of the current working directory, thus returning the contents of the same.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-example-4-loop-listdir-in-try-except-using-python\"><span class=\"ez-toc-section\" id=\"Example_4_loop_listdir_in_try-except_using_Python\"><\/span>Example 4: loop listdir in try-except using Python:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef loop():\n       try:\n        for txt in os.listdir(path):\n            if txt.endswith(txt_extension):\n                print(path + txt)\n    except KeyboardInterrupt:\n        exit()\n<\/pre><\/div>\n\n\n<p>In this code, we have <a href=\"http:\/\/www.pythonpool.com\/python-iterate-through-list\/\" target=\"_blank\" rel=\"noreferrer noopener\">iterated<\/a> through the directory&#8217;s contents to look for the files having &#8216;txt_extension&#8217; inside the try-except block. If the file having the specified extension is received, its name and path are printed. We have used the inbuilt <a href=\"http:\/\/www.pythonpool.com\/keyboardinterrupt\/\" target=\"_blank\" rel=\"noopener\">KeyboardInterrupt<\/a> exception. This exception is raised when the user presses an interrupt key like ctrl+c.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-example-5-python-listdir-sort-by-date\"><span class=\"ez-toc-section\" id=\"Example_5_Python_listdir_sort_by_date\"><\/span>Example 5: Python listdir sort by date:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport glob\nimport os\n\nfile = glob.glob(&quot;*.ipynb&quot;)\nfile.sort(key=os.path.getmtime)\nprint(&quot;\\n&quot;.join(files))\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-output-explanation-3\">Output &amp; Explanation:<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"381\" height=\"290\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-71.png\" alt=\"listdir sort by date\" class=\"wp-image-7984\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-71.png 381w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-71-300x228.png 300w\" sizes=\"(max-width: 381px) 100vw, 381px\" \/><figcaption class=\"wp-element-caption\"><strong><em>Output<\/em><\/strong><\/figcaption><\/figure><\/div>\n\n\n<p>We have used the <a href=\"https:\/\/docs.python.org\/3\/library\/glob.html\" target=\"_blank\" rel=\"noreferrer noopener\">glob module<\/a>, which is used to find all the pathnames matching a specified pattern. We then used the sort() method to sort the <a href=\"http:\/\/www.pythonpool.com\/python-get-filename-from-path\/\" target=\"_blank\" rel=\"noreferrer noopener\">files with the extension &#8216;ipynb&#8217;<\/a> according to time. By default, the result is sorted by name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-example-6-python-listdir-sort-by-name\"><span class=\"ez-toc-section\" id=\"Example_6_Python_listdir_sort_by_name\"><\/span>Example 6: Python listdir sort by name:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os \ndirct = os.listdir() \n\nprint(&quot;Files and directories:&quot;) \nprint(dirct.sort()) \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-output-explanation-4\">Output &amp; Explanation:<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-57.png\" alt=\"Output of listdir() without passing the parameter\" width=\"824\" height=\"212\"\/><figcaption class=\"wp-element-caption\"><strong><em>Output<\/em><\/strong><\/figcaption><\/figure><\/div>\n\n\n<p>Here, we have used the sort() method to sort the contents of the directory as per the name. Anyway, by default, the output of os.listdir() will be sorted as per the name only.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-example-7-python-listdir-absolute-path\"><span class=\"ez-toc-section\" id=\"Example_7_Python_listdir_absolute_path\"><\/span>Example 7: Python listdir absolute path:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport os\nabsp = os.path.abspath(os.getcwd())\nprint(&quot;Full path: &quot; + absp)\nprint(&quot;Directory Path: &quot; + os.path.dirname(absp))\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-output-explanation-5\">Output &amp; Explanation:<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"593\" height=\"91\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-72.png\" alt=\"absolute path\" class=\"wp-image-7985\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-72.png 593w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-72-300x46.png 300w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><figcaption class=\"wp-element-caption\"><strong><em>Output<\/em><\/strong><\/figcaption><\/figure><\/div>\n\n\n<p>Here, we have used the abspath() method to get the absolute path of the current directory. We then used the dirname() method to get the given absolute path&#8217;s directory path.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-example-8-python-listdir-with-specific-extension\"><span class=\"ez-toc-section\" id=\"Example_8_Python_listdir_with_specific_extension\"><\/span>Example 8: Python listdir with specific extension:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport glob\nimport os\n\nfile = glob.glob(&quot;*.ipynb&quot;)\nfile.sort(key=os.path.getmtime)\nprint(&quot;\\n&quot;.join(files))\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-output-explanation-6\">Output &amp; Explanation:<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/image-71.png\" alt=\"specific extension\"\/><figcaption class=\"wp-element-caption\"><strong><em>Output<\/em><\/strong><\/figcaption><\/figure><\/div>\n\n\n<p>As we can see, here we have extracted only the files having the extension &#8216;ipynb&#8217; using the glob module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion-python-os-listdir\"><span class=\"ez-toc-section\" id=\"Conclusion_Python_oslistdir\"><\/span>Conclusion | Python os.listdir():<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>With this, we come to an end to this article. I hope it was easy to understand the concept of listdir() in python and the various ways in which it can be used to extract the contents at a given path.<\/p>\n\n\n\n<p>However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.<\/p>\n\n\n\n<p><strong><em>Happy Pythoning!<\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello coders!! This article will be learning about the os.listdir function in Python. The os module allows us to use the functionality dependent on the &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"8 Examples to Implement os.listdir() in Python\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-listdir\/#more-7707\" aria-label=\"More on 8 Examples to Implement os.listdir() in Python\">Read more<\/a><\/p>\n","protected":false},"author":12,"featured_media":8222,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[3361,3360,3357,3362,3414,3415,3417,3359,3418,3358,3356,3416],"class_list":["post-7707","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-os-listdir-2","tag-os-listdir-full-path","tag-os-listdir-python","tag-os-listdir-python-3","tag-os-listdir-python-example","tag-os-listdir-sort","tag-os-listdir-wildcard","tag-os-listdir","tag-os-listdirdirectory","tag-os-listdirpath","tag-python-os-listdir","tag-python-os-listdir-example","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>8 Examples to Implement os.listdir() in Python - Python Pool<\/title>\n<meta name=\"description\" content=\"python listdir() is a method in the os module. It returns a list containing the contents of the directory given by path. Synatx: os.listdir(path)\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-listdir\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"8 Examples to Implement os.listdir() in Python\" \/>\n<meta property=\"og:description\" content=\"Hello coders!! This article will be learning about the os.listdir function in Python. The os module allows us to use the functionality dependent on the\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-listdir\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-26T10:12:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-07T08:50:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-40.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1350\" \/>\n\t<meta property=\"og:image:height\" content=\"650\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Prachee Sao\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prachee Sao\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-listdir\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-listdir\/\"},\"author\":{\"name\":\"Prachee Sao\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/b91d749826b21e606d55cda77d51ef47\"},\"headline\":\"8 Examples to Implement os.listdir() in Python\",\"datePublished\":\"2021-01-26T10:12:58+00:00\",\"dateModified\":\"2023-05-07T08:50:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-listdir\/\"},\"wordCount\":611,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-listdir\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-40.png\",\"keywords\":[\"os.listdir\",\"os.listdir full path\",\"os.listdir python\",\"os.listdir python 3\",\"os.listdir python example\",\"os.listdir sort\",\"os.listdir wildcard\",\"os.listdir()\",\"os.listdir(directory)\",\"os.listdir(path)\",\"python os.listdir\",\"python os.listdir example\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-listdir\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/python-listdir\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-listdir\/\",\"name\":\"8 Examples to Implement os.listdir() in Python - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-listdir\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-listdir\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-40.png\",\"datePublished\":\"2021-01-26T10:12:58+00:00\",\"dateModified\":\"2023-05-07T08:50:39+00:00\",\"description\":\"python listdir() is a method in the os module. It returns a list containing the contents of the directory given by path. Synatx: os.listdir(path)\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-listdir\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-listdir\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-listdir\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-40.png\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-40.png\",\"width\":1350,\"height\":650,\"caption\":\"8 Examples to Implement os.listdir() in Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-listdir\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"8 Examples to Implement os.listdir() in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/b91d749826b21e606d55cda77d51ef47\",\"name\":\"Prachee Sao\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d018c3758049ab2511d0772ac1f73c338aaa8c921577f39e0f1e5716fc7efcb4?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d018c3758049ab2511d0772ac1f73c338aaa8c921577f39e0f1e5716fc7efcb4?s=96&d=wavatar&r=g\",\"caption\":\"Prachee Sao\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"8 Examples to Implement os.listdir() in Python - Python Pool","description":"python listdir() is a method in the os module. It returns a list containing the contents of the directory given by path. Synatx: os.listdir(path)","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-listdir\/","og_locale":"en_US","og_type":"article","og_title":"8 Examples to Implement os.listdir() in Python","og_description":"Hello coders!! This article will be learning about the os.listdir function in Python. The os module allows us to use the functionality dependent on the","og_url":"https:\/\/www.pythonpool.com\/python-listdir\/","og_site_name":"Python Pool","article_published_time":"2021-01-26T10:12:58+00:00","article_modified_time":"2023-05-07T08:50:39+00:00","og_image":[{"width":1350,"height":650,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-40.png","type":"image\/png"}],"author":"Prachee Sao","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Prachee Sao","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-listdir\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-listdir\/"},"author":{"name":"Prachee Sao","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/b91d749826b21e606d55cda77d51ef47"},"headline":"8 Examples to Implement os.listdir() in Python","datePublished":"2021-01-26T10:12:58+00:00","dateModified":"2023-05-07T08:50:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-listdir\/"},"wordCount":611,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-listdir\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-40.png","keywords":["os.listdir","os.listdir full path","os.listdir python","os.listdir python 3","os.listdir python example","os.listdir sort","os.listdir wildcard","os.listdir()","os.listdir(directory)","os.listdir(path)","python os.listdir","python os.listdir example"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-listdir\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-listdir\/","url":"https:\/\/www.pythonpool.com\/python-listdir\/","name":"8 Examples to Implement os.listdir() in Python - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-listdir\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-listdir\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-40.png","datePublished":"2021-01-26T10:12:58+00:00","dateModified":"2023-05-07T08:50:39+00:00","description":"python listdir() is a method in the os module. It returns a list containing the contents of the directory given by path. Synatx: os.listdir(path)","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-listdir\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-listdir\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-listdir\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-40.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/01\/Theatre-Actor-Portfolio-Website-40.png","width":1350,"height":650,"caption":"8 Examples to Implement os.listdir() in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-listdir\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"8 Examples to Implement os.listdir() in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/b91d749826b21e606d55cda77d51ef47","name":"Prachee Sao","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d018c3758049ab2511d0772ac1f73c338aaa8c921577f39e0f1e5716fc7efcb4?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d018c3758049ab2511d0772ac1f73c338aaa8c921577f39e0f1e5716fc7efcb4?s=96&d=wavatar&r=g","caption":"Prachee Sao"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/7707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=7707"}],"version-history":[{"count":18,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/7707\/revisions"}],"predecessor-version":[{"id":27753,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/7707\/revisions\/27753"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/8222"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=7707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=7707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=7707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}