{"id":2104,"date":"2015-01-23T08:37:26","date_gmt":"2015-01-23T08:37:26","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2104"},"modified":"2015-01-23T08:38:16","modified_gmt":"2015-01-23T08:38:16","slug":"recursive-directory-walker-in-python","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/01\/23\/recursive-directory-walker-in-python\/","title":{"rendered":"Recursive directory walker in Python"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPython.js\"><\/script>\n<p>This code does a recursive listing of files and folders from a specified path. Though there are built in functions like glob() which can do this in one line, this is more to demonstrate recursion and also more control over the iteration.<\/p>\n<p>&nbsp;<\/p>\n<p>If no path is specified in the command line then it starts from the current working directory.<\/p>\n<p>eg.python listdir.py &#8220;\/var\/myfolder&#8221;<br \/>\n<pre class=\"brush: python\">import os\r\nimport sys\r\n\r\n#the main function to display and recurse\r\ndef walkdir(xpath):\r\n   global depth\r\n\r\n   for files in os.listdir(xpath):\r\n     if os.path.isfile(xpath + &#039;\/&#039; + files):\r\n        print spaces(depth) + files\r\n     else:\r\n         print spaces(depth) + &quot;\/&quot; + files\r\n         if os.path.isdir(xpath + &#039;\/&#039; + files):\r\n             depth += 1\r\n             walkdir(xpath + &#039;\/&#039; + files)\r\n   depth -= 1\r\n\r\n#display spaces to provide indentation to the output\r\ndef spaces(num):\r\n  retval = &#039;&#039;\r\n  for i in range (1,num):\r\n    retval += &#039;      &#039;\r\n  return retval\r\n\r\npath = &#039;&#039;\r\ndepth = 0\r\n\r\nif (sys.argv[1:] == []):\r\n  path = os.getcwd()\r\nelse:\r\n  path = sys.argv[1]\r\nprint path\r\nwalkdir(path)\r\n<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This code does a recursive listing of files and folders from a specified path. Though there are built in functions like glob() which can do <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/01\/23\/recursive-directory-walker-in-python\/\" title=\"Recursive directory walker in Python\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2107,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[297],"tags":[],"class_list":["post-2104","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2104"}],"version-history":[{"count":2,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2104\/revisions"}],"predecessor-version":[{"id":2106,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2104\/revisions\/2106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2107"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}