Hi.<div><br></div><div>I have an assigment where <span style="background-color:rgb(255,255,255);color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px">I'm need to write a module containing a function to examine the contents of the current working directory and print out a count of how many files have each extension (".txt", ".doc", etc.)</span><span style="background-color:rgb(255,255,255);color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px"> </span></div>
<span style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><div><span style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br>
</span></div>I am a beginner in Python.  </span><div><br></div><div><span style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">This is the code of the function module: </span><br style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<br style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><span style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">--- </span><br style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">import os </span><br style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">from collections import Counter </span><br style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">  </span><br style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<span style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">path = ":c//mypath/dir" </span><br style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<div class="GGC-W4ACF3" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(80,0,80);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">dirs = os.listdir( path ) <br>
filenames = {"this.txt", "that.txt", "the_other.txt","this.doc","that.doc","this.pdf","first.txt","that.pdf"} <br>extensions = [] <br>for filename in filenames: <br>
    f = open(filename, "w") <br>    f.write("Some text\n") <br>    f.close() <br>    name , ext = os.path.splitext(<a href="http://f.name/" target="_blank" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;text-decoration:none;color:rgb(102,17,204)">f.name</a>) <br>
    extensions.append(ext) <br><br># This would print all the files and directories <br>for file in dirs: <br>    print(file) <br><br><br><br></div><div class="GGC-W4ACF3" style="margin:0px;padding:0px;border:0px;vertical-align:baseline;color:rgb(80,0,80);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
for ext, count in Counter(extensions).items(): <br>    print("Count for %s: " % ext, count) <br><br></div><span style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">--- </span><br style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<br>path is just an example - not real path.<br><br style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><span style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">I need to make this module into a function and write a separate unittest module to verify by testing that the function gives correct results. </span><br style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<br style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><span style="color:rgb(34,34,34);font-family:Arial,Helvetica,sans-serif;font-size:13px;background-color:rgb(255,255,255)">Help and pointers are much appreciated. </span> <br clear="all">
<div><br></div>-- <br>Sukhvinder Singh<br>+4740633099<br>
</div>