{"id":133837,"date":"2023-05-20T19:37:50","date_gmt":"2023-05-20T19:37:50","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=1378590"},"modified":"2023-05-20T19:37:50","modified_gmt":"2023-05-20T19:37:50","slug":"python-get-all-txt-files-in-a-folder","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/05\/20\/python-get-all-txt-files-in-a-folder\/","title":{"rendered":"Python Get All TXT Files in a Folder"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1378590&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Python Get All TXT Files in a Folder&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/p><\/div>\n<p>Imagine you have a project that requires you to process tons of text files, and these files are scattered throughout your folder hierarchy. By the time you finish reading this article, you&#8217;ll be equipped with the knowledge to efficiently <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/find-all-files-in-directory-with-extension-txt\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/find-all-files-in-directory-with-extension-txt\/\" target=\"_blank\">fetch all the <code>.txt<\/code> files<\/a> in any folder using Python.<\/p>\n<h2 class=\"wp-block-heading\">Method 1: The Os Module<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">The <code>os<\/code> module can be used to interact effectively with the file system. The method <code>os.listdir()<\/code> lists all files and directories in your target folder. You&#8217;ll use this method along with a <code>for<\/code> loop and the <code>endswith()<\/code> method to filter <code>.txt<\/code> files specifically. <\/p>\n<p>Here&#8217;s the code snippet:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"6-7\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os directory = '.\/your_folder\/'\ntxt_files = [] for file in os.listdir(directory): if file.endswith('.txt'): txt_files.append(file) print(txt_files)\n<\/pre>\n<p>This code imports the <code>os<\/code> module, sets the target directory, and initializes an <a href=\"https:\/\/blog.finxter.com\/how-to-create-an-empty-list-in-python\/\" data-type=\"post\" data-id=\"453870\" target=\"_blank\" rel=\"noreferrer noopener\">empty list<\/a>. <\/p>\n<p>The <code>for<\/code> loop iterates through all the files and checks for the <code>.txt<\/code> extension using the <code><a href=\"https:\/\/blog.finxter.com\/python-string-endswith\/\" data-type=\"post\" data-id=\"26009\" target=\"_blank\" rel=\"noreferrer noopener\">endswith()<\/a><\/code> method. Matching files are added to the list, which is printed at the end.<\/p>\n<h2 class=\"wp-block-heading\">Method 2: The Glob Module (My Fav <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ab.png\" alt=\"\ud83d\udcab\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>)<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"679\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-310-1024x679.png\" alt=\"\" class=\"wp-image-1378627\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-310-1024x679.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-310-300x199.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-310-768x509.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-310.png 1309w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p class=\"has-global-color-8-background-color has-background\">Another solution involves using the <code>glob<\/code> module, which allows you to find all the file paths in a directory that match a specific pattern. You can use the <code>glob.glob()<\/code> function to list all <code>.txt<\/code> files. <\/p>\n<p>Here&#8217;s how you can do it:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import glob directory = '.\/your_folder\/'\ntxt_files = glob.glob(f'{directory}*.txt') print(txt_files)\n<\/pre>\n<p>This method imports the <code>glob<\/code> module, sets the target directory, and retrieves the list of text files using the <code>glob.glob()<\/code> function that filters file paths based on the given pattern (<code>*.txt<\/code>). The list of <code>.txt<\/code> files is then printed.<\/p>\n<h2 class=\"wp-block-heading\">Method 3: os.listdir() and List Comprehension<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">The <code>os.listdir()<\/code> is a simple method to use when listing all files in a directory. You can iterate over all files obtain with this method using a simple <a href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"post\" data-id=\"1171\" target=\"_blank\" rel=\"noreferrer noopener\">list comprehension<\/a> statement such as <code>[file for file in os.listdir(dir_path) if file.endswith(\".txt\")]<\/code>. <\/p>\n<p>See this example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4-5\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os dir_path = \"your_directory_path\"\nall_files = os.listdir(dir_path)\ntxt_files = [file for file in all_files if file.endswith(\".txt\")] print(txt_files)\n<\/pre>\n<p>This code will list all the text files in the specified directory using <code>os.listdir<\/code> function.<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4c3.png\" alt=\"\ud83d\udcc3\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2 class=\"wp-block-heading\">Method 4: Using os.scandir()<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">The <code>os.scandir()<\/code> method can provide more information about each file. Extracting the files from this more information-rich representation is a bit less concise but works just fine in this list comprehension <code>[entry.name for entry in os.scandir(dir_path) if entry.name.endswith(\".txt\") and entry.is_file()]<\/code>.<\/p>\n<p>For instance, use the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os dir_path = \"your_directory_path\"\ntxt_files = [entry.name for entry in os.scandir(dir_path) if entry.name.endswith(\".txt\") and entry.is_file()] print(txt_files)\n<\/pre>\n<h2 class=\"wp-block-heading\">Method 5: Using glob.glob()<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"609\" height=\"916\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-311.png\" alt=\"\" class=\"wp-image-1378628\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-311.png 609w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-311-199x300.png 199w\" sizes=\"auto, (max-width: 609px) 100vw, 609px\" \/><\/figure>\n<\/div>\n<p>For a more concise solution, try the <code>glob.glob()<\/code> function from the <code>glob<\/code> library. Here&#8217;s the code snippet to list text files:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"4\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import glob dir_path = \"your_directory_path\"\ntxt_files = glob.glob(f\"{dir_path}\/*.txt\") print(txt_files)\n<\/pre>\n<p>The <code>glob.glob()<\/code> function returns a list of all text files with the specified pattern (in this case, <code>*.txt<\/code>).<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2728.png\" alt=\"\u2728\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2 class=\"wp-block-heading\">Method 6: Using pathlib.Path.iterdir()<\/h2>\n<p>Finally, the <code>pathlib.Path.iterdir<\/code> method offers another way to list text files in a directory. To use this method, simply import the <code>pathlib<\/code> library and write the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from pathlib import Path dir_path = Path(\"your_directory_path\")\ntxt_files = [file.name for file in dir_path.iterdir() if file.is_file() and file.name.endswith(\".txt\")] print(txt_files)\n<\/pre>\n<p>In this code, <code>pathlib.Path.iterdir<\/code> is iterator over the files in the directory and, when combined with list comprehensions, can efficiently list all text files.<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f389.png\" alt=\"\ud83c\udf89\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2 class=\"wp-block-heading\">Iterating Through Directories<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"643\" height=\"915\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-312.png\" alt=\"\" class=\"wp-image-1378631\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-312.png 643w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-312-211x300.png 211w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/figure>\n<\/div>\n<p>In this section, you&#8217;ll learn how to iterate through directories using Python and get all the <code>.txt<\/code> files in a folder. <\/p>\n<p>We&#8217;ll cover three methods: using the <code>for<\/code> loop method, working with the <code><a href=\"https:\/\/blog.finxter.com\/python-os-walk-a-simple-illustrated-guide\/\" data-type=\"post\" data-id=\"248394\" target=\"_blank\" rel=\"noreferrer noopener\">os.walk()<\/a><\/code> function, and <a href=\"https:\/\/blog.finxter.com\/i-used-this-python-script-to-rename-all-files-in-subfolders-recursive\/\" data-type=\"post\" data-id=\"1046210\" target=\"_blank\" rel=\"noreferrer noopener\">recursively traversing directories<\/a> with a custom recursive function. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4c1.png\" alt=\"\ud83d\udcc1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h3 class=\"wp-block-heading\">Using the For Loop Method<\/h3>\n<p class=\"has-global-color-8-background-color has-background\">To get started, we&#8217;ll use the <code>os.listdir()<\/code> function with a <code>for<\/code> loop. This approach allows you to iterate over all files in a directory and filter by their extension. <\/p>\n<p>This code lists all the <code>.txt<\/code> files in the specified directory using a simple <code>for<\/code> loop. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f44d.png\" alt=\"\ud83d\udc4d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os directory = 'your_directory_path'\nfor filename in os.listdir(directory): if filename.endswith('.txt'): print(os.path.join(directory, filename))\n<\/pre>\n<\/p>\n<h3 class=\"wp-block-heading\">Working with the os.walk() Function<\/h3>\n<p class=\"has-global-color-8-background-color has-background\">The <code>os.walk()<\/code> function is another powerful tool for iterating over files in directories. It enables you to traverse a directory tree and retrieve all files with a specific extension:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os root_dir = 'your_directory_path'\nfor root, dirs, files in os.walk(root_dir): for file in files: if file.endswith('.txt'): print(os.path.join(root, file))\n<\/pre>\n<p>This code explores the entire directory tree, including subdirectories, and prints out the full paths of <code>.txt<\/code> files. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f333.png\" alt=\"\ud83c\udf33\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p>In fact, we have written a detailed article with a video on the function, feel free to check it out! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/python-get-all-txt-files-in-a-folder\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FenbzcMTml3o%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<p class=\"has-base-2-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f9d1-200d-1f4bb.png\" alt=\"\ud83e\uddd1\u200d\ud83d\udcbb\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Recommended<\/strong>: <a href=\"https:\/\/blog.finxter.com\/python-os-walk-a-simple-illustrated-guide\/\" data-type=\"post\" data-id=\"248394\" target=\"_blank\" rel=\"noreferrer noopener\">Python os.walk() \u2013 A Simple Illustrated Guide<\/a><\/p>\n<h3 class=\"wp-block-heading\">Recursively Traversing Directories with a Recursive Function<\/h3>\n<p>Lastly, you could create a custom recursive function to traverse directories and collect <code>.txt<\/code> files. This method is particularly useful when working with different operating systems, like Windows and Unix:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from pathlib import Path def find_txt_files(path: Path): txt_files = [] for item in path.iterdir(): if item.is_dir(): txt_files.extend(find_txt_files(item)) elif item.name.endswith('.txt'): txt_files.append(item) return txt_files directory = Path('your_directory_path')\ntxt_files = find_txt_files(directory)\nprint(txt_files)\n<\/pre>\n<p>This recursive function explores directories and subdirectories and returns a list of <code>.txt<\/code> files. This approach is more versatile as it leverages Python 3&#8217;s <code>pathlib<\/code> module. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f501.png\" alt=\"\ud83d\udd01\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2 class=\"wp-block-heading\">Filtering Based on File Extension and Size<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"682\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-313-1024x682.png\" alt=\"\" class=\"wp-image-1378632\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-313-1024x682.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-313-300x200.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-313-768x511.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/05\/image-313.png 1308w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p>To get all the <code>.txt<\/code> files in a folder, you can use the <code>glob<\/code> module in Python, which provides an easy way to find files matching a specific pattern. <\/p>\n<p>Here&#8217;s a simple code snippet to get started:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import glob txt_files = glob.glob('path\/to\/your\/folder\/*.txt')\nprint(txt_files)\n<\/pre>\n<p>This code will provide the absolute paths of all the <code>.txt<\/code> files within the specified folder. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4c1.png\" alt=\"\ud83d\udcc1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p>Now that you have the <code>.txt<\/code> files, you might want to filter them based on their size. To achieve this, you can use the <code>os<\/code> module. <\/p>\n<p>Here&#8217;s an example of how to filter <code>.txt<\/code> files by size:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nimport glob min_size = 1000 # Replace with your desired minimum file size in bytes txt_files = glob.glob('path\/to\/your\/folder\/*.txt')\nfiltered_files = [file for file in txt_files if os.path.getsize(file) >= min_size] print(filtered_files)\n<\/pre>\n<p>In this code, <code>min_size<\/code> represents the minimum file size in bytes that you wish to retrieve. By using a <a href=\"https:\/\/blog.finxter.com\/how-to-filter-a-list-in-python\/\" data-type=\"post\" data-id=\"7586\" target=\"_blank\" rel=\"noreferrer noopener\">list comprehension with a condition<\/a>, you can filter out the files that don&#8217;t meet your size requirements. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4cf.png\" alt=\"\ud83d\udccf\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p>If you want to find <code>.txt<\/code> files not only in the target folder but also within its subdirectories, you can use the <code>**<\/code> pattern along with the <code>recursive<\/code> parameter:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">txt_files = glob.glob('path\/to\/your\/folder\/**\/*.txt', recursive=True)\n<\/pre>\n<p>Using this approach, you can easily tailor your search to retrieve specific <code>.txt<\/code> files based on their size and location. With these tools at hand, you should be able to efficiently filter files in your Python projects. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f40d.png\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2 class=\"wp-block-heading\">Operating System Compatibility<\/h2>\n<p>Python works well across different operating systems, including Unix and Windows. Thanks to its compatibility <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f91d.png\" alt=\"\ud83e\udd1d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>, <em>you<\/em> can consistently use your code on different platforms. For this task, both the <code>os<\/code> and <code>glob<\/code> libraries are compatible with Unix and Windows systems, so you don&#8217;t have to worry about your text file retrieval code failing on either OS.<\/p>\n<p class=\"has-global-color-8-background-color has-background\">To get all the text files in a folder using Python, you can use the <code>os<\/code> and <code>glob<\/code> libraries. This works for all operating systems, i.e., Linux, Windows, Ubuntu, macOS. <\/p>\n<p>Here&#8217;s a code snippet to achieve this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nimport glob os.chdir(\"your_directory_path\")\ntxt_files = glob.glob('*.txt')\nprint(txt_files)\n<\/pre>\n<p>Replace &#8220;<code>your_directory_path<\/code>&#8221; with the path of your folder containing the txt files.<\/p>\n<h2 class=\"wp-block-heading\">Recommended Video<\/h2>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/python-get-all-txt-files-in-a-folder\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FNiLhQy_iFU4%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Imagine you have a project that requires you to process tons of text files, and these files are scattered throughout your folder hierarchy. By the time you finish reading this article, you&#8217;ll be equipped with the knowledge to efficiently fetch all the .txt files in any folder using Python. Method 1: [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-133837","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/133837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=133837"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/133837\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=133837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=133837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=133837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}