Python: getting filename case as stored in Windows?

前端 未结 6 1698
终归单人心
终归单人心 2020-12-17 15:01

Though Windows is case insensitive, it does preserve case in filenames. In Python, is there any way to get a filename with case as it is stored on the file system?

6条回答
  •  忘掉有多难
    2020-12-17 15:07

    You could use:

    import os
    a = os.listdir('mydirpath')
    b = [f.lower() for f in a]
    try:
        i = b.index('texas.txt')
        print a[i]
    except ValueError:
        print('File not found in this directory')
    

    This of course assumes that your search string 'texas.txt' is in lowercase. If it isn't you'll have to convert it to lowercase first.

提交回复
热议问题