Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28636

Re: Function for examine content of directory

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 <mailman.309.1346944827.27098.python-list@python.org>
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 <feae1d5a-c477-4e21-8136-c14e38672cb9@googlegroups.com> <mailman.309.1346944827.27098.python-list@python.org>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <08938014-e4e0-4f6d-b11a-e843ed4a0da3@googlegroups.com> (permalink)
Subject Re: Function for examine content of directory
From Tigerstyle <laddosingh@gmail.com>
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

Show key headers only | View raw


Thanks, just what I was looking for :-)

T

kl. 17:20:27 UTC+2 torsdag 6. september 2012 skrev MRAB følgende:
> On 06/09/2012 15:56, Tigerstyle wrote:
> 
> > Hi guys,
> 
> >
> 
> > I'm trying 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.)
> 
> >
> 
> > This is the code so far:
> 
> > --
> 
> > import os
> 
> >
> 
> > path = "v:\\workspace\\Python2_Homework03\\src\\"
> 
> > dirs = os.listdir( path )
> 
> > filenames = {"this.txt", "that.txt", "the_other.txt","this.doc","that.doc","this.pdf","first.txt","that.pdf"}
> 
> > extensions = []
> 
> > for filename in filenames:
> 
> >      f = open(filename, "w")
> 
> >      f.write("Some text\n")
> 
> >      f.close()
> 
> >      name , ext = os.path.splitext(f.name)
> 
> >      extensions.append(ext)
> 
> >
> 
> > # This would print all the files and directories
> 
> > for file in dirs:
> 
> >      print(file)
> 
> >
> 
> > for ext in extensions:
> 
> >      print("Count for %s: " %ext, extensions.count(ext))
> 
> >
> 
> > --
> 
> >
> 
> > When I'm trying to get the module to print how many files each extension has, it prints the count of each ext multiple times for each extension type. Like this:
> 
> >
> 
> > this.pdf
> 
> > the_other.txt
> 
> > this.doc
> 
> > that.txt
> 
> > this.txt
> 
> > that.pdf
> 
> > first.txt
> 
> > that.doc
> 
> > Count for .pdf:  2
> 
> > Count for .txt:  4
> 
> > Count for .doc:  2
> 
> > Count for .txt:  4
> 
> > Count for .txt:  4
> 
> > Count for .pdf:  2
> 
> > Count for .txt:  4
> 
> > Count for .doc:  2
> 
> >
> 
> That's because each extension can occur multiple times in the list.
> 
> 
> 
> Try the Counter class:
> 
> 
> 
> from collections import Counter
> 
> 
> 
> for ext, count in Counter(extensions).items():
> 
>      print("Count for %s: " % ext, count)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Function for examine content of directory Tigerstyle <laddosingh@gmail.com> - 2012-09-06 07:56 -0700
  Re: Function for examine content of directory Ian Foote <ian@feete.org> - 2012-09-06 16:06 +0100
  Re: Function for examine content of directory MRAB <python@mrabarnett.plus.com> - 2012-09-06 16:20 +0100
    Re: Function for examine content of directory Tigerstyle <laddosingh@gmail.com> - 2012-09-06 13:26 -0700
    Re: Function for examine content of directory Tigerstyle <laddosingh@gmail.com> - 2012-09-06 13:26 -0700
  Re: Function for examine content of directory Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-06 17:26 -0400
  Re: Function for examine content of directory Chris Angelico <rosuav@gmail.com> - 2012-09-07 07:48 +1000
  Re: Function for examine content of directory Tigerstyle <laddosingh@gmail.com> - 2012-09-07 07:23 -0700
  Re: Function for examine content of directory Tigerstyle <laddosingh@gmail.com> - 2012-09-07 07:28 -0700
    Re: Function for examine content of directory Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-07 15:21 -0400

csiph-web