Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'f.close()': 0.07; 'filename': 0.07; 'filenames': 0.07; '"w")': 0.09; 'filenames:': 0.09; 'subject:Function': 0.09; 'extension': 0.13; 'extensions': 0.13; 'duplicates': 0.16; 'from:addr:ian': 0.16; 'guys,': 0.16; 'set()': 0.16; 'skip:" 70': 0.16; 'wrote:': 0.17; 'module': 0.19; 'skip:" 40': 0.20; 'trying': 0.21; 'import': 0.21; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'ext': 0.27; 'list:': 0.27; 'received:208.97': 0.29; 'received:208.97.132': 0.29; 'received:dreamhost.com': 0.29; 'received:g.dreamhost.com': 0.29; "i'm": 0.29; 'function': 0.30; 'code': 0.31; 'etc.)': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'path': 0.35; 'should': 0.36; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'you.': 0.61; 'containing': 0.61; 'received:208': 0.63; 'here': 0.65; 'subject:content': 0.84; 'examine': 0.95 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=feete.org; h=message-id :date:from:mime-version:to:subject:references:in-reply-to :content-type:content-transfer-encoding; s=feete.org; bh=gkXaKMF HmkY//pDtMDj3TqREPNI=; b=b49IHvecp+JKCKl6a3+Gyvd/WfOHs8g3YiYaCEb 6y3daJYFc9KNidhbv+HOBi4fwcc212z9O5K6/aqyP+mg6y/XiSDjlaZgv6/PT4Vm FuSLsrNClbocgel8LTcHKobHbttDH5eqh/ve9trY373GF86T38gmXx/gFiue2+jt WzhI= Date: Thu, 06 Sep 2012 16:06:35 +0100 From: Ian Foote User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120827 Thunderbird/15.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Function for examine content of directory References: In-Reply-To: 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 List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346944006 news.xs4all.nl 6945 [2001:888:2000:d::a6]:34936 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28607 On 06/09/12 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 = [] Try using a set here instead of a list: extensions = set() > 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) and use: extensions.add(ext) This should take care of duplicates for you. Regards, Ian