Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'mrab': 0.05; 'f.close()': 0.07; 'filename': 0.07; 'filenames': 0.07; '"w")': 0.09; 'collections': 0.09; 'filenames:': 0.09; 'subject:Function': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'extension': 0.13; 'extensions': 0.13; ':-)': 0.13; 'class:': 0.16; 'count)': 0.16; 'extensions:': 0.16; 'guys,': 0.16; 'skip:" 70': 0.16; 'wrote:': 0.17; 'thanks,': 0.18; 'module': 0.19; 'skip:" 40': 0.20; 'trying': 0.21; 'import': 0.21; 'cc:2**0': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'ext': 0.27; 'received:209.85.212': 0.28; 'prints': 0.29; "i'm": 0.29; 'function': 0.30; 'code': 0.31; 'file': 0.32; 'etc.)': 0.32; 'print': 0.32; 'received:google.com': 0.34; 'path': 0.35; 'received:209.85': 0.35; 'list.': 0.35; 'received:209': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'containing': 0.61; 'times': 0.63; 'subject:content': 0.84; 'received:209.85.212.56': 0.91; 'examine': 0.95 Newsgroups: comp.lang.python Date: Thu, 6 Sep 2012 13:26:32 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=81.166.233.176; posting-account=04fc6goAAACDYxuhiIAKatXLjxcOghf2 References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 81.166.233.176 MIME-Version: 1.0 Subject: Re: Function for examine content of directory From: Tigerstyle To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 122 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346963199 news.xs4all.nl 6900 [2001:888:2000:d::a6]:53968 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28637 Thanks, just what I was looking for :-) T kl. 17:20:27 UTC+2 torsdag 6. september 2012 skrev MRAB f=F8lgende: > On 06/09/2012 15:56, Tigerstyle wrote: >=20 > > Hi guys, >=20 > > >=20 > > I'm trying to write a module containing a function to examine the conte= nts of the current working directory and print out a count of how many file= s have each extension (".txt", ".doc", etc.) >=20 > > >=20 > > This is the code so far: >=20 > > -- >=20 > > import os >=20 > > >=20 > > path =3D "v:\\workspace\\Python2_Homework03\\src\\" >=20 > > dirs =3D os.listdir( path ) >=20 > > filenames =3D {"this.txt", "that.txt", "the_other.txt","this.doc","that= .doc","this.pdf","first.txt","that.pdf"} >=20 > > extensions =3D [] >=20 > > for filename in filenames: >=20 > > f =3D open(filename, "w") >=20 > > f.write("Some text\n") >=20 > > f.close() >=20 > > name , ext =3D os.path.splitext(f.name) >=20 > > extensions.append(ext) >=20 > > >=20 > > # This would print all the files and directories >=20 > > for file in dirs: >=20 > > print(file) >=20 > > >=20 > > for ext in extensions: >=20 > > print("Count for %s: " %ext, extensions.count(ext)) >=20 > > >=20 > > -- >=20 > > >=20 > > When I'm trying to get the module to print how many files each extensio= n has, it prints the count of each ext multiple times for each extension ty= pe. Like this: >=20 > > >=20 > > this.pdf >=20 > > the_other.txt >=20 > > this.doc >=20 > > that.txt >=20 > > this.txt >=20 > > that.pdf >=20 > > first.txt >=20 > > that.doc >=20 > > Count for .pdf: 2 >=20 > > Count for .txt: 4 >=20 > > Count for .doc: 2 >=20 > > Count for .txt: 4 >=20 > > Count for .txt: 4 >=20 > > Count for .pdf: 2 >=20 > > Count for .txt: 4 >=20 > > Count for .doc: 2 >=20 > > >=20 > That's because each extension can occur multiple times in the list. >=20 >=20 >=20 > Try the Counter class: >=20 >=20 >=20 > from collections import Counter >=20 >=20 >=20 > for ext, count in Counter(extensions).items(): >=20 > print("Count for %s: " % ext, count)