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


Groups > comp.lang.python > #64553

Re: Case insensitive exists()?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@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; 'tree': 0.05; 'string': 0.09; 'differing': 0.09; 'filename': 0.09; 'filenames': 0.09; 'iterate': 0.09; 'os.path': 0.09; 'windows,': 0.09; 'bug': 0.12; 'jan': 0.12; 'windows': 0.15; '23,': 0.16; "can't.": 0.16; 'dict': 0.16; 'for,': 0.16; 'formatted': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'insensitive': 0.16; 'retrieving': 0.16; 'roy': 0.16; 'subject:Case': 0.16; 'subject:exists': 0.16; 'subject:insensitive': 0.16; 'there?': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'differ': 0.19; 'thu,': 0.19; '(in': 0.22; 'to:name:python-list@python.org': 0.22; 'this?': 0.23; 'instance,': 0.24; 'string,': 0.24; 'compare': 0.26; 'switch': 0.26; '(for': 0.26; 'header:In-Reply-To:1': 0.27; 'matching': 0.30; 'message-id:@mail.gmail.com': 0.30; 'existence': 0.31; 'larry': 0.31; 'anyone': 0.31; 'file': 0.32; 'probably': 0.32; 'ago': 0.33; 'actual': 0.34; 'maybe': 0.34; 'could': 0.34; "can't": 0.35; 'created': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'example,': 0.37; 'two': 0.37; 'mapping': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'pm,': 0.38; 'rather': 0.38; 'does': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'system.': 0.39; 'worry': 0.60; 'guarantee': 0.63; 'name': 0.63; 'happen': 0.63; 'map': 0.64; 'different': 0.65; 'due': 0.66; 'case?': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=mJjt/vAlatF9dbfX6Tm9wTwKUTrD8YPzc37USgIiINU=; b=P/iSWBAaaJzMfAvm3DQ2mTjKeMMIX/FXGs/O/8KY/njuYDwiekVtTcgkK+6jHJGsZ6 wnt3CpYWFzQP6gLsciqHYPgirol+Gxz7XWUnK/kiiTnqiSUkZOtArhfFkLQpXZPfEzUz WdrNZx8vC3B6wUfa+0X3K4tdDGjg1GnIWaZQwg+n7YW6EeVUQLszMX1Xhvv0VWIyesob 5ju8ExdRs8cn7YcBNAF0YYYca9oFmjaQWjWr3boRsudHsAXB6NqUJsgl5l0AWwigjIDt ogX3821N5w+gCvgnvqpyIUjxsjf5gDRvy+oKscTe12kkmas02i9opsgKdMWEXfjjas9B cjfA==
MIME-Version 1.0
X-Received by 10.66.102.39 with SMTP id fl7mr5237265pab.43.1390443654715; Wed, 22 Jan 2014 18:20:54 -0800 (PST)
In-Reply-To <52E07014.2010203@mrabarnett.plus.com>
References <CACwCsY45rqTQO+AGw6029g_i91zeP9uwWaGTQ4WtbaZgTtptEQ@mail.gmail.com> <52E07014.2010203@mrabarnett.plus.com>
Date Thu, 23 Jan 2014 13:20:54 +1100
Subject Re: Case insensitive exists()?
From Chris Angelico <rosuav@gmail.com>
To "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5858.1390443664.18130.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1390443664 news.xs4all.nl 2833 [2001:888:2000:d::a6]:43881
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64553

Show key headers only | View raw


On Thu, Jan 23, 2014 at 12:27 PM, MRAB <python@mrabarnett.plus.com> wrote:
> On 2014-01-23 00: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?
>>
> You don't say which OS. Filenames in Windows, for example, are already
> case-insensitive.

There are weird use-cases for case insensitive filename matching on a
case sensitive file system. (For instance, a program that parses a
playlist file created on Windows. I did that a while ago - had to map
"Foobar.MID" to "FooBar.mid", matching case insensitively and
retrieving the actual case used on the FS.)

A good data structure is probably all you need. As Roy suggests,
iterate once over the directories - for instance, create a dict
mapping the .lower() of the filename to the original. Of course, then
you have to worry about collisions... though you may be able to
guarantee that they can't happen (in the above case, I was looking
through a directory tree that was unzipped straight from the Windows
collection, ergo no two filenames could differ only in case). Or maybe
you *think* you can guarantee it, but due to a bug you can't. (Ever
had a USB stick, formatted NTFS, with two files differing only in
case? Windows XP behaves... oddly. And rather amusingly.)

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Case insensitive exists()? Chris Angelico <rosuav@gmail.com> - 2014-01-23 13:20 +1100

csiph-web