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


Groups > comp.lang.python > #28637

Re: Function for examine content of directory

Newsgroups comp.lang.python
Date 2012-09-06 13:26 -0700
References <feae1d5a-c477-4e21-8136-c14e38672cb9@googlegroups.com> <mailman.309.1346944827.27098.python-list@python.org>
Subject Re: Function for examine content of directory
From Tigerstyle <laddosingh@gmail.com>
Message-ID <mailman.326.1346963199.27098.python-list@python.org> (permalink)

Show all headers | 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