{"id":1368,"date":"2013-07-11T12:44:25","date_gmt":"2013-07-11T12:44:25","guid":{"rendered":"http:\/\/garysieling.com\/blog\/?p=1368"},"modified":"2013-07-11T12:44:25","modified_gmt":"2013-07-11T12:44:25","slug":"python-directory-list-index","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/","title":{"rendered":"Building an Directory Structure Index in Python"},"content":{"rendered":"<p>I&#8217;m working through examples in &#8220;<a href=\"http:\/\/www.amazon.com\/gp\/product\/0596516495\/ref=as_li_ss_tl?ie=UTF8&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0596516495&#038;linkCode=as2&#038;tag=thesecrelifeo-20\">Natural Language Processing with Python<\/a>&#8221; (<a href=\"http:\/\/garysieling.com\/blog\/book-review-natural-language-processing-with-python\">read my review<\/a>) and found that the corpus I have to work with is large enough to require special performance tuning exercises.<\/p>\n<p>If you have a large enough directory structure, it becomes difficult to walk with os.walk &#8211; for instance any failure in longer scripts require starting from scratch. This is a common issue in larger systems &#8211; typically they manage file listings through a relational database, and directory storage is obfuscated in some way.<\/p>\n<p><a href=\"http:\/\/garysieling.com\/blog\/visualizing-six-million-documents\">In this environment<\/a> it takes at an hour for Windows to count the files, and python seems to take longer.<\/p>\n<p>It&#8217;s worth generating a list of the files in advance &#8211; this lists which PDFs and HTML documents exist, and for which a <a href=\"http:\/\/garysieling.com\/blog\/scraping-pdf-text-with-python\">text extract has been generated<\/a> (<a href=\"http:\/\/garysieling.com\/blog\/parsing-pdfs-at-scale-with-node-js-pdf-js-and-lunr-js\">see a Node.JS approach here<\/a>). This supports a few uses, including generating missing text renditions.<\/p>\n<pre lang=\"python\">\n\nimport os\nimport re\nimport datetime\n\nprint datetime.datetime.now()\n\npdf_idx = open('pdfs.idx', 'w')\nrendition_idx = open('txts.idx', 'w')\nhtml_idx = open('htmls.idx', 'w')\nxml_idx = open('xmls.idx', 'w')\n\nfor root, dirs, files in os.walk('.'):\n  for f in files:\n    if f.endswith(\".pdf\"):\n      rend = root + os.sep + f + \".textrendition.txt\"\n      try:\n          with open(rend):\n              rendition_idx.write(rend + \"\\n\")\n      except IOError:\n          pass\n      pdf_idx.write(root + os.sep + f + \"\\n\")\n    if f.endswith(\".html\") or f.endswith('.htm'):\n      html_idx.write(root + os.sep + f + \"\\n\")\n    if f.endswith(\".xml\"):\n      xml_idx.write(root + os.sep + f + \"\\n\")      \n\nrendition_idx.close()\npdf_idx.close()\nhtml_idx.close()\nxml_idx.close()\n\nprint datetime.datetime.now()\n<\/pre>\n<p>What this allows is quite useful &#8211; you can read the file quickly, and select a random subset for training and test data for NLP algorithms. This could also be done by storing all the names in a database, but this is probably the simplest and fastest for my current needs.<\/p>\n<pre lang=\"python\">\nrendition_idx = open('txts1.idx', 'r')\nfiles = [f[:-1] for f in rendition_idx]\nrendition_idx.close()\nlen(files)\n124559\n\n>>> files[:4]\n['.\/00\/00\/gov.uscourts.rid.6064\/gov.uscourts.rid.6064.20.0.pdf.textrendition.txt', \n'.\/00\/01\/gov.uscourts.cacd.547806\/gov.uscourts.cacd.547806.6.0.pdf.textrendition.txt', \n'.\/00\/01\/gov.uscourts.oknd.31699\/gov.uscourts.oknd.31699.21.0.pdf.textrendition.txt', \n'.\/00\/01\/gov.uscourts.paed.406890\/gov.uscourts.paed.406890.19.0.pdf.textrendition.txt']\n\nimport random\nrandom.shuffle(files)\nfiles[:4]\n['.\/16\/63\/gov.uscourts.ded.48575\/gov.uscourts.ded.48575.1.0.pdf.textrendition.txt', \n'.\/09\/51\/gov.uscourts.casd.273674\/gov.uscourts.casd.273674.1.0.pdf.textrendition.txt', \n'.\/09\/aa\/gov.uscourts.hid.14739\/gov.uscourts.hid.14739.73.0.pdf.textrendition.txt', \n'.\/09\/57\/gov.uscourts.casd.268361\/gov.uscourts.casd.268361.4.0.pdf.textrendition.txt']\n\ncontents = [open(f[2:]).read() for f in files[:100]]\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m working through examples in &#8220;Natural Language Processing with Python&#8221; (read my review) and found that the corpus I have to work with is large enough to require special performance tuning exercises. If you have a large enough directory structure, it becomes difficult to walk with os.walk &#8211; for instance any failure in longer scripts &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Building an Directory Structure Index in Python&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[4,5,7],"tags":[385,421,447],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"I&#039;m working through examples in &quot;Natural Language Processing with Python&quot; (read my review) and found that the corpus I have to work with is large enough to require special performance tuning exercises. If you have a large enough directory structure, it becomes difficult to walk with os.walk - for instance any failure in longer scripts\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"gary\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Gary Sieling - Software Engineer\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Building an Directory Structure Index in Python - Gary Sieling\" \/>\n\t\t<meta property=\"og:description\" content=\"I&#039;m working through examples in &quot;Natural Language Processing with Python&quot; (read my review) and found that the corpus I have to work with is large enough to require special performance tuning exercises. If you have a large enough directory structure, it becomes difficult to walk with os.walk - for instance any failure in longer scripts\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2013-07-11T12:44:25+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2013-07-11T12:44:25+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Building an Directory Structure Index in Python - Gary Sieling\" \/>\n\t\t<meta name=\"twitter:description\" content=\"I&#039;m working through examples in &quot;Natural Language Processing with Python&quot; (read my review) and found that the corpus I have to work with is large enough to require special performance tuning exercises. If you have a large enough directory structure, it becomes difficult to walk with os.walk - for instance any failure in longer scripts\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/python-directory-list-index\\\/#blogposting\",\"name\":\"Building an Directory Structure Index in Python - Gary Sieling\",\"headline\":\"Building an Directory Structure Index in Python\",\"author\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\"},\"datePublished\":\"2013-07-11T12:44:25+00:00\",\"dateModified\":\"2013-07-11T12:44:25+00:00\",\"inLanguage\":\"en-US\",\"commentCount\":2,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/python-directory-list-index\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/python-directory-list-index\\\/#webpage\"},\"articleSection\":\"Code Examples, Data Mining, Database Performance Tuning, nlp, performance, python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/python-directory-list-index\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.garysieling.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/#listItem\",\"name\":\"Code Examples\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/#listItem\",\"position\":2,\"name\":\"Code Examples\",\"item\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/python-directory-list-index\\\/#listItem\",\"name\":\"Building an Directory Structure Index in Python\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/python-directory-list-index\\\/#listItem\",\"position\":3,\"name\":\"Building an Directory Structure Index in Python\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/#listItem\",\"name\":\"Code Examples\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\",\"name\":\"Gary Sieling\",\"description\":\"Software Engineer\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/\",\"name\":\"gary\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/python-directory-list-index\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/0be925276d848ffe98a6a9dc8cf33e67?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"gary\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/python-directory-list-index\\\/#webpage\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/python-directory-list-index\\\/\",\"name\":\"Building an Directory Structure Index in Python - Gary Sieling\",\"description\":\"I'm working through examples in \\\"Natural Language Processing with Python\\\" (read my review) and found that the corpus I have to work with is large enough to require special performance tuning exercises. If you have a large enough directory structure, it becomes difficult to walk with os.walk - for instance any failure in longer scripts\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/python-directory-list-index\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"datePublished\":\"2013-07-11T12:44:25+00:00\",\"dateModified\":\"2013-07-11T12:44:25+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/\",\"name\":\"Gary Sieling\",\"description\":\"Software Engineer\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Building an Directory Structure Index in Python - Gary Sieling","description":"I'm working through examples in \"Natural Language Processing with Python\" (read my review) and found that the corpus I have to work with is large enough to require special performance tuning exercises. If you have a large enough directory structure, it becomes difficult to walk with os.walk - for instance any failure in longer scripts","canonical_url":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/#blogposting","name":"Building an Directory Structure Index in Python - Gary Sieling","headline":"Building an Directory Structure Index in Python","author":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"publisher":{"@id":"https:\/\/www.garysieling.com\/blog\/#organization"},"datePublished":"2013-07-11T12:44:25+00:00","dateModified":"2013-07-11T12:44:25+00:00","inLanguage":"en-US","commentCount":2,"mainEntityOfPage":{"@id":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/#webpage"},"isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/#webpage"},"articleSection":"Code Examples, Data Mining, Database Performance Tuning, nlp, performance, python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.garysieling.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/#listItem","name":"Code Examples"}},{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/#listItem","position":2,"name":"Code Examples","item":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/#listItem","name":"Building an Directory Structure Index in Python"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/#listItem","position":3,"name":"Building an Directory Structure Index in Python","previousItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/#listItem","name":"Code Examples"}}]},{"@type":"Organization","@id":"https:\/\/www.garysieling.com\/blog\/#organization","name":"Gary Sieling","description":"Software Engineer","url":"https:\/\/www.garysieling.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author","url":"https:\/\/www.garysieling.com\/blog\/author\/gary\/","name":"gary","image":{"@type":"ImageObject","@id":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/0be925276d848ffe98a6a9dc8cf33e67?s=96&d=identicon&r=g","width":96,"height":96,"caption":"gary"}},{"@type":"WebPage","@id":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/#webpage","url":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/","name":"Building an Directory Structure Index in Python - Gary Sieling","description":"I'm working through examples in \"Natural Language Processing with Python\" (read my review) and found that the corpus I have to work with is large enough to require special performance tuning exercises. If you have a large enough directory structure, it becomes difficult to walk with os.walk - for instance any failure in longer scripts","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/#breadcrumblist"},"author":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"creator":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"datePublished":"2013-07-11T12:44:25+00:00","dateModified":"2013-07-11T12:44:25+00:00"},{"@type":"WebSite","@id":"https:\/\/www.garysieling.com\/blog\/#website","url":"https:\/\/www.garysieling.com\/blog\/","name":"Gary Sieling","description":"Software Engineer","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.garysieling.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Gary Sieling - Software Engineer","og:type":"article","og:title":"Building an Directory Structure Index in Python - Gary Sieling","og:description":"I'm working through examples in &quot;Natural Language Processing with Python&quot; (read my review) and found that the corpus I have to work with is large enough to require special performance tuning exercises. If you have a large enough directory structure, it becomes difficult to walk with os.walk - for instance any failure in longer scripts","og:url":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/","article:published_time":"2013-07-11T12:44:25+00:00","article:modified_time":"2013-07-11T12:44:25+00:00","twitter:card":"summary_large_image","twitter:title":"Building an Directory Structure Index in Python - Gary Sieling","twitter:description":"I'm working through examples in &quot;Natural Language Processing with Python&quot; (read my review) and found that the corpus I have to work with is large enough to require special performance tuning exercises. If you have a large enough directory structure, it becomes difficult to walk with os.walk - for instance any failure in longer scripts"},"aioseo_meta_data":{"post_id":"1368","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2023-02-04 16:18:28","updated":"2026-07-06 00:58:49","ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.garysieling.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/\" title=\"Code Examples\">Code Examples<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tBuilding an Directory Structure Index in Python\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.garysieling.com\/blog"},{"label":"Code Examples","link":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/"},{"label":"Building an Directory Structure Index in Python","link":"https:\/\/www.garysieling.com\/blog\/python-directory-list-index\/"}],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1368"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=1368"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1368\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=1368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=1368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=1368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}