Case insensitive exists()?

Tim Chase python.list at tim.thechases.com
Thu Jan 23 07:48:42 EST 2014


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






More information about the Python-list mailing list