Accessing a path which case sensitive without writing so
<p>I would like to know whether it possible to access linux path like:
<code>/home/dan/CaseSensitivE/test.txt</code></p>

<p>In a way we write it as <code>/home/dan/casesensitive/test.txt</code> and it goes to the right place, means python consider paths as not case sensitive and allow entering them that way, although they are case sensitive.</p>

<p>As Klaus said, the simple answer is no. You could, however, take a more laborious route, and enumerate all folders/files in your top directory (<code>os.path, glob</code>), convert to lower case (<code>string.lower</code>), test equality, step one level down, etc.</p>

<p>This works for me:</p>

<pre><code>import os

def match_lowercase_path(path):
    # get absolute path
    path = os.path.abspath(path)

    # try it first
    if os.path.exists(path):
        correct_path = path
    # no luck
    else:
        # works on linux, but there must be a better way
        components = path.split('/')

        # initialise answer
        correct_path = '/'

        # step through
        for c in components:
            if os.path.isdir(correct_path + c):
                correct_path += c +'/'
            elif os.path.isfile(correct_path + c):
                correct_path += c
            else:
                match = find_match(correct_path, c)
                correct_path += match

    return correct_path

def find_match(path, ext):
    for child in os.listdir(path):
        if child.lower() == ext:
            if os.path.isdir(path + child):
                return child + '/'
            else:
                return child
    else:
        raise ValueError('Could not find a match for {}.'.format(path + ext))
</code></pre>

python python-2.7