Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28609
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python@mrabarnett.plus.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'f.close()': 0.07; 'filename': 0.07; 'filenames': 0.07; '"w")': 0.09; 'collections': 0.09; 'filenames:': 0.09; 'subject:Function': 0.09; 'extension': 0.13; 'extensions': 0.13; 'class:': 0.16; 'count)': 0.16; 'extensions:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'guys,': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'skip:" 70': 0.16; 'wrote:': 0.17; 'module': 0.19; 'skip:" 40': 0.20; 'trying': 0.21; 'import': 0.21; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'ext': 0.27; 'prints': 0.29; 'received:192.168.1.3': 0.29; "i'm": 0.29; 'function': 0.30; 'code': 0.31; 'file': 0.32; 'etc.)': 0.32; 'received:84': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'path': 0.35; 'list.': 0.35; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'containing': 0.61; 'times': 0.63; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'reply-to:addr:python.org': 0.84; 'subject:content': 0.84; 'examine': 0.95 |
| X-CM-Score | 0.00 |
| X-CNFS-Analysis | v=2.0 cv=IekFqBWa c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=lR6CHUT36vYA:10 a=tUWFX44Y8y0A:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=Sq8MXgxk4qcA:10 a=R2ScKVH5wzZNGDypbRcA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 |
| X-AUTH | mrabarnett:2500 |
| Date | Thu, 06 Sep 2012 16:20:30 +0100 |
| From | MRAB <python@mrabarnett.plus.com> |
| User-Agent | Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120824 Thunderbird/15.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Function for examine content of directory |
| References | <feae1d5a-c477-4e21-8136-c14e38672cb9@googlegroups.com> |
| In-Reply-To | <feae1d5a-c477-4e21-8136-c14e38672cb9@googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| Reply-To | python-list@python.org |
| 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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.309.1346944827.27098.python-list@python.org> (permalink) |
| Lines | 57 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1346944827 news.xs4all.nl 6891 [2001:888:2000:d::a6]:44117 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:28609 |
Show key headers only | View raw
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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