Received: by 10.224.181.75 with SMTP id bx11mr1921342qab.7.1346963192737; Thu, 06 Sep 2012 13:26:32 -0700 (PDT) Received: by 10.52.90.129 with SMTP id bw1mr674830vdb.13.1346963192700; Thu, 06 Sep 2012 13:26:32 -0700 (PDT) Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newspeer1.nac.net!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!b19no1292194qas.0!news-out.google.com!da15ni851qab.0!nntp.google.com!b19no1292192qas.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail 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 NNTP-Posting-Host: 81.166.233.176 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <08938014-e4e0-4f6d-b11a-e843ed4a0da3@googlegroups.com> Subject: Re: Function for examine content of directory From: Tigerstyle Cc: python-list@python.org Injection-Date: Thu, 06 Sep 2012 20:26:32 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Lines: 121 Xref: csiph.com comp.lang.python:28636 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)