OS X上的文件名不区分大小写 [英] Filename case-insensitivity on OS X

查看:86
本文介绍了OS X上的文件名不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午全部,


MacOS X似乎对于

路径中的案例价值有一些异端的想法 - 它似乎相信它没有'存在,或多或少,所以触摸

foo FOO触摸只有一个文件,你不能同时拥有''makefile''和

''Makefile''在同一个目录中,

" os.path。存在(some_valid_path.upper())"即使在

os.listdir(os.path.split(some_valid_path)中

&os; os.path.split(some_valid_path.upper())[1]时返回True [0])"返回False等等

(当然,ls * .txt并没有提到任何那些.TXT文件

躺在那里)。 />

只是为了证明这一点,这就是unix(特别是linux)的作用:


twic @ urchin:〜$ uname

Linux

twic @ urchin:〜$ python

Python 2.3.5(#2,2005年9月4日,22:01:42)

[GCC 3.3.5(Debian 1:3.3.5-13)] on linux2

输入help,copyright,credit等等。或许可证或更多信息。

Afternoon all,

MacOS X seems to have some heretical ideas about the value of case in
paths - it seems to believe that it doesn''t exist, more or less, so "touch
foo FOO" touches just one file, you can''t have both ''makefile'' and
''Makefile'' in the same directory,
"os.path.exists(some_valid_path.upper())" returns True even when
"os.path.split(some_valid_path.upper())[1] in
os.listdir(os.path.split(some_valid_path)[0])" returns False, etc
(although, of course, "ls *.txt" doesn''t mention any of those .TXT files
lying around).

Just to prove it, here''s what unix (specifically, linux) does:

twic@urchin:~$ uname
Linux
twic@urchin:~$ python
Python 2.3.5 (#2, Sep 4 2005, 22:01:42)
[GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import os
filenames = os.listdir("。")
first = filenames [0]
首先在文件名中
在文件名中为true first.upper()
False os.path.exists(os.path.join("。",first) )
True os.path.exists(os.path.join("。",first.upper()))
False


这里OS X的作用是:


胡克:〜汤姆$ uname

达尔文

胡克:~tom $ python

Python 2.4.1(#2,2005年3月31日,00:05:10)

[GCC 3.3 20030304(Apple Computer,Inc。build 1666)]在darwin上br />
输入help,copyright,credit等等。或许可证或欲获得更多信息。 import os
filenames = os.listdir("。")
first = filenames [0]
first in filenames
True first.upper()in filenames
假os.path.exists(os.path.join("。",first))
True os.path.exists(os.path.join("。",first.upper( )))
True
import os
filenames = os.listdir(".")
first = filenames[0]
first in filenames True first.upper() in filenames False os.path.exists(os.path.join(".", first)) True os.path.exists(os.path.join(".", first.upper())) False
And here''s what OS X does:

Hooke:~ tom$ uname
Darwin
Hooke:~ tom$ python
Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
Type "help", "copyright", "credits" or "license" for more information. import os
filenames = os.listdir(".")
first = filenames[0]
first in filenames True first.upper() in filenames False os.path.exists(os.path.join(".", first)) True os.path.exists(os.path.join(".", first.upper())) True




叹息。任何人都有解决这个问题的好主意,特别是os.path.exists的
?我希望有一些os.path.actualpath,所以我

可以说:


def exists_dontignorecase(路径):

返回os.path.exists(path)和(path == os.path.actualpath(path))


Java有一个java.io.File.getCanonicalPath方法来执行此操作,但是我的b $ b在python中找不到相应的东西 - 有吗?


我可以这样仿效:


def _canonicalise(s,l):

s = s.lower()

for t in l:

if s == t.lower():

返回t

引发ValueError,(无法规范化字符串,s)


def canonicalpath(path):

if(path in(" /\",,\"\"\"\"))):

返回路径

parent,child = os.path.split(path)

cparent = canonicalpath(parent)

cchild = _canonicalise(child,os.listdir(cparent))

返回os.path.join(cparent,cchild)


或者更粗略地做一些像thi这样的事情s:


def exists_dontignorecase(路径):

dir,f = os.path.split(路径)

返回f在os.listdir(dir)


但欢迎更好的解决方案。


谢谢,

tom


-

步兵犯,步兵死亡。炮兵犯,步兵死亡。 - IDF谚语



Sigh. Anyone got any bright ideas for working around this, specifically
for os.path.exists? I was hoping there was some os.path.actualpath, so i
could say:

def exists_dontignorecase(path):
return os.path.exists(path) and (path == os.path.actualpath(path))

Java has a java.io.File.getCanonicalPath method that does this, but i
can''t find an equivalent in python - is there one?

I can emulate it like this:

def _canonicalise(s, l):
s = s.lower()
for t in l:
if s == t.lower():
return t
raise ValueError, ("could not canonicalise string", s)

def canonicalpath(path):
if (path in ("/", "")):
return path
parent, child = os.path.split(path)
cparent = canonicalpath(parent)
cchild = _canonicalise(child, os.listdir(cparent))
return os.path.join(cparent, cchild)

Or, more crudely, do something like this:

def exists_dontignorecase(path):
dir, f = os.path.split(path)
return f in os.listdir(dir)

But better solutions are welcome.

Thanks,
tom

--
Infantry err, infantry die. Artillery err, infantry die. -- IDF proverb

推荐答案

uname

Linux

twic @ urchin:〜
uname
Linux
twic@urchin:~


python

Python 2。3。5(#2,2005年9月4日,22:01:42)

[GCC 3.3.5( Debian 1:3.3.5-13)] on linux2

输入help,copyright,credit等等。或许可证或更多信息。
python
Python 2.3.5 (#2, Sep 4 2005, 22:01:42)
[GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import os
filenames = os.listdir("。")
first = filenames [0]
首先在文件名中
在文件名中为true first.upper()
False os.path.exists(os.path.join("。",first) )
True os.path.exists(os.path.join("。",first.upper()))
False


这里OS X的作用是:


胡克:〜汤姆
import os
filenames = os.listdir(".")
first = filenames[0]
first in filenames True first.upper() in filenames False os.path.exists(os.path.join(".", first)) True os.path.exists(os.path.join(".", first.upper())) False
And here''s what OS X does:

Hooke:~ tom


uname

达尔文

胡克:~tom
uname
Darwin
Hooke:~ tom


这篇关于OS X上的文件名不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆