{"id":14285,"date":"2021-12-14T23:35:13","date_gmt":"2021-12-14T23:35:13","guid":{"rendered":"https:\/\/lgildv5i97.onrocket.site\/answers\/?post_type=question&#038;p=14285"},"modified":"2021-12-14T23:35:58","modified_gmt":"2021-12-14T23:35:58","slug":"python-count-file-by-extension","status":"publish","type":"question","link":"https:\/\/computing.net\/answers\/programming\/python-count-file-by-extension\/20831.html","title":{"rendered":"Python &#8211; Count File By Extension"},"content":{"rendered":"<p>Hi,<\/p>\n<p>I was trying to write a python script to take in a directory, recursive scan it for all files, keep track the number of files with a particular extension and print how many files of each extension type there were.<\/p>\n<p>Unfortunately I&#8217;m getting the following error:<\/p>\n<pre>Traceback (most recent call last):\r\n  File \"H:\\search.py\", line 30, in &lt;module&gt;\r\n    extensionarray = check_format(filearray)\r\n  File \"H:\\search.py\", line 18, in check_format\r\n    val = index(result1, extension)\r\n  File \"H:\\search.py\", line 13, in index\r\n    return next((i for i in xrange(len(seq)) if f(seq[i])), 'None')\r\n  File \"H:\\search.py\", line 13, in &lt;genexpr&gt;\r\n    return next((i for i in xrange(len(seq)) if f(seq[i])), 'None')\r\nTypeError: 'str' object is not callable<\/pre>\n<p>I would appreciate any help, this was what I have so far:<\/p>\n<p>&nbsp;<\/p>\n<pre>import os, sys\r\n\r\ndef get_all_files(srchpath):\r\n\tallfiles = []\r\n\tfor root, dirs, files in os.walk(srchpath):\r\n\t\tfor file in files:\r\n\t\t\tpathname = os.path.join(root, file)\r\n\t\t\textension = os.path.splitext(file)[1]\r\n\t\t\tallfiles.append([file, pathname, extension])\r\n\treturn allfiles\r\n\t\r\ndef index(seq, f):\r\n    return next((i for i in xrange(len(seq)) if f(seq[i])), 'None')\r\n\t\r\ndef check_format(thefiles):\r\n\tresult1 = []\r\n\tfor filename, pathname, extension in thefiles:\r\n\t\tval = index(result1, extension)\r\n\t\tif val == 'None':\r\n\t\t\tresult1.append([extension, 0])\r\n\t\telse:\r\n\t\t\tresult1[val][1] += 1\r\n\treturn result1\r\n\r\ndef print_array(thearray):\r\n\tfor val in range(1, len(thearray)):\r\n\t\tprint thearray[val]\r\n\r\nfilearray = get_all_files(sys.argv[1])\r\nextensionarray = check_format(filearray)\r\nprint_array(extensionarray)<\/pre>\n<p>** thanks nbrane for catching my first mistake!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"iawp_total_views":4},"question-category":[55],"question_tags":[],"class_list":["post-14285","question","type-question","status-publish","hentry","question-category-programming"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question\/14285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question"}],"about":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/comments?post=14285"}],"wp:attachment":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/media?parent=14285"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question-category?post=14285"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question_tags?post=14285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}