我确定这是故意的,所以有人可以解释这种行为的理由:
Python 2.7.2 (default, Oct 13 2011, 15:27:47)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from os.path import isdir,expanduser
>>> isdir("~amosa/pdb")
False
>>> isdir(expanduser("~amosa/pdb"))
True
>>>
>>> from os import chdir
>>> chdir("~amosa/pdb")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: '~amosa/pdb'
>>> chdir(expanduser("~amosa/pdb"))
>>>
这真的很烦人,因为毕竟可以明确解析其中包含用户名的路径......我想编写可以处理用户可能给我的任何类型输入的代码,但是这种行为需要我调用 expanduser我的代码必须处理的每条路径。这也意味着在我打印出该路径供用户查看的任何地方,它的可读性都会比他们给我的略差。
这似乎与“鸭子打字”的概念不一致,我概括地说我希望python不会对我发牢骚,除非真的有问题......