{"id":13224,"date":"2025-08-19T07:07:08","date_gmt":"2025-08-19T07:07:08","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=13224"},"modified":"2025-08-19T07:07:08","modified_gmt":"2025-08-19T07:07:08","slug":"list-all-files-of-a-directory","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/python\/list-all-files-of-a-directory","title":{"rendered":"How to List All Files of a Directory?"},"content":{"rendered":"<p>Use os.listdir() or pathlib.Path.iterdir()<br \/>\nYou can list all files in a directory and store them in a list using these methods:<\/p>\n<h3>Method 1: Using os.listdir() with filtering<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import os\r\ndirectory = '\/path\/to\/directory'\r\nfiles = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]\r\nprint(files)\r\n<\/pre>\n<h3>Method 2: Using os.walk() to get all files recursively<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nimport os\r\ndirectory = '\/path\/to\/directory'\r\nfiles = []\r\nfor dirpath, _, filenames in os.walk(directory):\r\n   for f in filenames:\r\n       files.append(os.path.join(dirpath, f))\r\nprint(files)\r\n<\/pre>\n<h3>Method 3: Using pathlib (Python 3.4+)<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nfrom pathlib import Path\r\ndirectory = Path('\/path\/to\/directory')\r\nfiles = [f.name for f in directory.iterdir() if f.is_file()]\r\nprint(files)\r\n<\/pre>\n<h3>Optional: Get full paths<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\n# pathlib\r\nfiles = [str(f.resolve()) for f in directory.iterdir() if f.is_file()]\r\n# os\r\nfiles = [os.path.join(directory, f) for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]\r\n<\/pre>\n<p><strong>Notes:<\/strong><\/p>\n<ul>\n<li>Use os.listdir() for simple cases.<\/li>\n<li>Use os.walk() if you want to include files from subdirectories.<\/li>\n<li>Prefer pathlib for cleaner and more readable code in modern Python.<\/li>\n<\/ul>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/what-is-python-used-for\" target=\"_blank\">Why Use Python<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Use os.listdir() or pathlib.Path.iterdir() You can list all files in a directory and store them in a list using these methods: Method 1: Using os.listdir() with filtering import os directory = &#8216;\/path\/to\/directory&#8217; files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))] print(files) Method 2: Using os.walk() to get all files recursively import os directory [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13225,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-13224","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13224"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=13224"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13224\/revisions"}],"predecessor-version":[{"id":13226,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13224\/revisions\/13226"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/13225"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=13224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=13224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=13224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}