Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.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.000 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; 'name)': 0.09; 'path.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sep': 0.09; 'sub': 0.09; 'subject:Function': 0.09; 'thread': 0.11; 'extension': 0.13; 'extensions': 0.13; '(like': 0.15; '.db': 0.16; '.py': 0.16; '.txt': 0.16; 'count)': 0.16; 'created.': 0.16; 'ext)': 0.16; 'grouped': 0.16; 'prefix,': 0.16; 'railroad': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'scratch': 0.16; 'simpler,': 0.16; 'skip:" 70': 0.16; 'string': 0.17; 'headers': 0.17; 'windows': 0.19; 'import': 0.21; 'defined': 0.22; '---': 0.26; 'skip:" 20': 0.26; 'raw': 0.27; 'ext': 0.27; 'header:X-Complaints-To:1': 0.28; 'run': 0.28; 'node': 0.29; 'prints': 0.29; 'probability': 0.29; 'src': 0.29; 'probably': 0.29; "i'm": 0.29; 'fri,': 0.30; 'query': 0.30; 'code': 0.31; 'file': 0.32; 'print': 0.32; 'pst': 0.33; 'url:home': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; 'invoice': 0.33; 'done': 0.34; 'list': 0.35; 'filter': 0.35; 'path': 0.35; 'received:org': 0.36; 'really': 0.36; 'created': 0.36; 'totally': 0.36; 'test': 0.36; 'should': 0.36; 'too': 0.36; 'charset:us- ascii': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'skip:n 10': 0.63; 'note:': 0.64; 'adc': 0.84; 'stereo': 0.84; 'subject:content': 0.84; 'care,': 0.91; 'dennis': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Function for examine content of directory Date: Fri, 07 Sep 2012 15:21:50 -0400 Organization: > Bestiaria Support Staff < References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-76-253-111-50.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES 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: 260 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347045722 news.xs4all.nl 6899 [2001:888:2000:d::a6]:53291 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28706 On Fri, 7 Sep 2012 07:28:03 -0700 (PDT), Tigerstyle declaimed the following in gmane.comp.python.general: > Ok I'm now totally stuck. > > This is the code: > This code is full of errors... > --- > import os > from collections import Counter > > path = ":c\\mypath\dir" Not a valid Windows path. The format should be "c:\mypath\dir" (actually, to use \ you should probably declare it a raw string -- much simpler, since all the python/OS functions don't care, is to use / -- as in "c:/mypath/dir") > dirs = os.listdir( path ) Warning, this will also list items that are not files (like subdirectories). (hence "dirs" is a misleading name) > 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) This prints the file/directory /name/ NOTE: you grabbed the list of names BEFORE you created your test data files, so... > > > > for ext, count in Counter(extensions).items(): > print("Count for %s: " % ext, count) > ... this is not really a count of files grouped by extension IN the directory -- this is only the count based on the file names you defined to be created. I'm not going to create test files, nor a test suite, and what I have done is still too much... but... -=-=-=-=- import os import collections PATH = "e:/userdata/wulfraed/my documents/python progs" fids = os.listdir(PATH) fids.sort() nmlen = max([len(f) for f in fids]) format = "%%%ss %%10s" % nmlen cntr = collections.Counter() for fid in fids: prefix, ext = os.path.splitext(fid) print format % (prefix, ext) cntr.update([ext]) print "\n\n" for ext, cnt in cntr.items(): print "%10s %10s" % (ext, cnt) -=-=-=-=- .project .pydevproject .settings ABA .py ADC .py BookList .zip CGIServer DGen .py DiskCatalog .py DiskCatalog .pyc Dload .py Firearms .csv GWhist .py HTML .py Hanoi .py Hanoi .pyc HierHead .py Intervals .py MBX_Split .py MySQLTest .py MySQLTest .pyc MySQLdb .html MySQLdb_files NIM1 .py NumberPrinter .py PhotoFrame .py Probability .py ProgressBar .py ProgressBar2 .py RandomScores .py SQL .py SQLiteTest .py SampleData .txt SampleFormat .tsv Script1 .py Script2 .py Script3 .py Script3 .pyc Sociable_Chain .py Sociable_Chain .pyc Stereo .py TAGS .py azel_interp .py binadd .py binadd2 .py bsddb-test .py cgiform .py chessclock .py counter .py counterthread .py cp .py data .txt databasetest .py databasetest2 .py dbfail .py dbg .py dbg .pyc dbtst .py dirwalk .py execsub .py extractor .py filecnt .py filter .py fulldicttest .py h2b .py h2b .pyc headers .py highScore .py htmlparse .py i2b .py i2b .pyc infile1 .tsv infile2 .tsv infile3 .tsv int2wrd .py int2wrd .pyc int2wrd2 .py int2wrd2 .pyc intervalfile .txt invoice .csv junk .py justify .py linkedlist .py llist .py main .py make_ou_class .py make_ou_class .pyc mileage .py minmax .py mofn .py mofn.py .zip movefiles .py moving .py mptest1 .py myhtmlparser .py myhtmlparser .pyc mytest .py mytest .pyc node .py node .pyc pcdtojpeg .py pst .py queens1 .py queens2 .py queens2.py .zip query .py railroad .py rpg .py run .py s .txt sample .tsv scramble .py scratch .db script1 .html script1 .sql script2 .html setuptools-0.6c6-py2.4 .egg sgml .py spam .py sqltest .py sqrot .py src sub .py sub_p1 .py sub_p3 .py sudoku .py sudoku.py .bak sudoku .pyc summup_dict1 summup_dict2 summup_dict2b summup_dict3 summup_list t .dat t .py tabspace .py tabspace .pyc tdriver .py test .csd test .db test .sql test .txt testABA .py testABA .pyc tgsetup .py thread .py threadsample .py threadswap .py timetest .py timing .py trips .dat update_log ut_00 .py wordprob .py 12 .pyc 17 .bak 1 .sql 2 .tsv 5 .csv 2 .db 2 .dat 2 .py 98 .txt 5 .html 3 .csd 1 .egg 1 .zip 3 -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/