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!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.04; 'string': 0.09; 'filename': 0.09; 'fname': 0.09; 'os.path': 0.09; 'cc:addr:python-list': 0.11; '-tkc': 0.16; 'dict(': 0.16; 'for,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'subject:Case': 0.16; 'subject:exists': 0.16; 'subject:insensitive': 0.16; 'there?': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'print': 0.22; 'this?': 0.23; 'string,': 0.24; 'cc:2**0': 0.24; 'compare': 0.26; 'switch': 0.26; 'header:In-Reply-To:1': 0.27; 'existence': 0.31; 'larry': 0.31; 'anyone': 0.31; 'file': 0.32; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'charset:us-ascii': 0.36; 'possible': 0.36; 'files': 0.38; 'does': 0.39; 'name': 0.63; 'different': 0.65; 'to:addr:gmail.com': 0.65; 'received:50.22': 0.84 Date: Thu, 23 Jan 2014 06:48:42 -0600 From: Tim Chase To: Larry Martell Subject: Re: Case insensitive exists()? In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-OutGoing-Spam-Status: No, score=-1.5 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390481274 news.xs4all.nl 2960 [2001:888:2000:d::a6]:59626 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64601 On 2014-01-22 17:58, Larry Martell wrote: > I have the need to check for a files existence against a string, > but I need to do case-insensitively. I cannot efficiently get the > name of every file in the dir and compare each with my string using > lower(), as I have 100's of strings to check for, each in a > different dir, and each dir can have 100's of files in it. Does > anyone know of an efficient way to do this? There's no switch for > os.path that makes exists() check case-insensitively is there? Is it possible to rephrase the problem in terms of a different algorithm that can be made case-insensitive? Something like from_db = set( row[0].upper() for row in db.execute( "select filename from tblfoo where ..." ).fetchall() ) from_fs = dict( (fname.upper(), os.path.join(pth, fname)) for pth, dirs, files in os.walk(ROOT) for fname in files ) common_filenames = from_db & set(from_fs) for fname in common_filenames: print from_fs[fname] -tkc