简体   繁体   English

调用Python的os.getcwd()时如何获得正确的大小写?

[英]How can I get the correct case when calling Python's os.getcwd()?

When I call os.getcwd() on my Mac under OS X 10.6.4 from my home directory, the path is converted to lowercase. 当我从主目录在OS X 10.6.4的Mac上的Mac上调用os.getcwd()时,路径将转换为小写字母。 I get 我明白了

/users/myusername /用户/名为myUsername

instead of 代替

/Users/myusername /用户/用户名为myusername

On another Mac I get the correct uppercase string. 在另一台Mac上,我得到正确的大写字符串。 How can I configure this? 我该如何配置? Is there a (hidden) Python setting? 有(隐藏的)Python设置吗?

Reason for asking: On OS X all user directories are in "/Users". 询问原因:在OS X上,所有用户目录都在“/ Users”中。 This is always uppercase. 这总是大写的。 When using Mercurial, it gets sometimes confused with this strange and incorrect normalization of my current directory. 当使用Mercurial时,它有时会与我当前目录的这种奇怪和错误的规范化混淆。

In reply to the suggested solutions: 在回复建议的解决方案时:

(1) DiskUtil says that my drive has a format of "Mac OS Extended (Journaled)". (1)DiskUtil说我的驱动器格式为“Mac OS Extended(Journaled)”。 I bought my computer some month ago and it's still the original drive, never reformatted. 我一个月前买了电脑,它仍然是原始驱动器,从未格式化。

(2) Another python console session log: (2)另一个python控制台会话日志:

$>>> os.system('pwd') $ >>> os.system('pwd')

/Users/klaas /用户/克拉斯

0 0

$>>> os.getcwd()" $ >>> os.getcwd()“

'/users/klaas' “/用户/克拉斯”

$>>> os.path.normcase('/Users/klaas') $ >>> os.path.normcase('/ Users / klaas')

'/Users/klaas' “/用户/克拉斯”

$>>> os.chdir('/UsErS/klaas') $ >>> os.chdir('/ UsErS / klaas')

$>>> os.getcwd() $ >>> os.getcwd()

'/users/klaas' “/用户/克拉斯”

$>>> os.chdir('/UsErS/klaas') $ >>> os.chdir('/ UsErS / klaas')

$>>> os.getcwd() $ >>> os.getcwd()

'/users/klaas' “/用户/克拉斯”

Update 2: 更新2:

Thanks for all the replies. 感谢所有的答复。 I created a test case in Objective-C and it has the same lowercase folder name: 我在Objective-C中创建了一个测试用例,它具有相同的小写文件夹名称:

NSFileManager *filemgr;
NSString *currentpath;

filemgr = [NSFileManager defaultManager];

currentpath = [filemgr currentDirectoryPath];

NSLog (@"Current directory is %@", currentpath);

The "HFS plus" filesystem, which has been Apple's filesystem of choice since the days of Mac OS 8, is normally not case-sensitive. 自Mac OS 8诞生以来,“ HFS plus”文件系统一直是Apple首选的文件系统,通常不区分大小写。

Mac OS X version 10.4, or "Tiger" as it's more commonly known, introduced the ability to create an HFS plus filesystem which is case-sensitive. Mac OS X版本10.4或俗称的“老虎”,引入了创建区分大小写的HFS plus文件系统的功能。

Could one of them be using a case-sensitive FS? 其中之一可以使用区分大小写的FS吗?

The problem seems to be a problem with your particular Python install on your particular Mac. 问题似乎是您在特定Mac上安装特定Python的问题。 I haven't been able to turn up anyone else who has the same problem. 我无法找到有同样问题的其他人。

It's likely that your Mac has a problem with its installation of Python. 您的Mac很可能在安装Python时遇到问题。 AFAIK, there isn't a configuration option to make Python represent getcwd() with the proper case (this behavior should be enabled by default). AFAIK,没有配置选项让Python用正确的大小写表示getcwd() (默认情况下应启用此行为)。 If it's an option, I'd try reinstalling Python. 如果它是一个选项,我会尝试重新安装Python。 If that doesn't work, you should report a bug to the Python bug tracker . 如果这不起作用,则应向Python错误跟踪器报告错误

Two uneducated guesses: 两个没有根据的猜测:

First, apparently OSX is usually case-insensitive, which usually means case-preserving: the name of the current directory either comes from how it's capitalized on disk, or how it was capitalized when you changed into it. 首先,显然OSX通常不区分大小写,这通常意味着要保留大小写:当前目录的名称要么来自磁盘上大写的方式,要么来自于更改时的大写方式。 If you run os.system('pwd') , does it show it lowercased or not? 如果你运行os.system('pwd') ,它是否显示小写? If so, then that's what the OS is reporting and Python isn't doing anything wrong. 如果是这样,那就是操作系统报告的内容,而Python没有做错任何事情。 Does it change if you os.chdir('/UsErS/username') first? 如果你os.chdir('/UsErS/username')它会改变吗?

Second, there's one function that does lowercase or not selectively based on the OS: os.path.normcase . 其次,有一个函数会根据操作系统选择是否执行小写操作: os.path.normcase The macpath version does lowercase. macpath版本确实是小写的。 I'm pretty sure os.getcwd does not automatically run the results through this; 我敢肯定os.getcwd 不会自动运行通过这个结果; just mentioning it as a place to check. 只是提到它作为一个检查的地方。

In the end it was an issue with the Mercurial installation. 最后,这是Mercurial安装的问题。 I ran into it again tonight. 今晚我又遇到了它。

https://www.mercurial-scm.org/wiki/Download gives you different options how to install Mercurial. https://www.mercurial-scm.org/wiki/Download为您提供了不同的安装Mercurial的选项。 If I choose the first one (the Mac OS X packages) the "/Users" directory will be reported as "/users" (even when accessing it with Objective C). 如果我选择第一个(Mac OS X软件包),“/ Users”目录将报告为“/ users”(即使使用Objective C访问它)。 This can be solved by installing Mercurial via the macports option. 这可以通过通过macports选项安装Mercurial来解决。 This fixes the issue and the "/Users" directory is returned again with an uppercase "U". 这解决了该问题,并且使用大写字母“ U”再次返回了“ / Users”目录。

Any hints why this is happening are still welcome. 仍然欢迎出现这种情况的任何提示。

But a least there is a solution. 但是至少有解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM