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


Groups > comp.lang.python > #28637

Re: Function for examine content of directory

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 <laddosingh@gmail.com>
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 <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
References <feae1d5a-c477-4e21-8136-c14e38672cb9@googlegroups.com> <mailman.309.1346944827.27098.python-list@python.org>
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 <laddosingh@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.326.1346963199.27098.python-list@python.org> (permalink)
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

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