[英]For what reason when I use environment variables returns error?
I expect both cases returned the same message, but only the first is correct 我希望两种情况都返回相同的消息,但只有第一种是正确的
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.isdir('/home/macabeus/ApenasMeu')
True
>>> os.path.isdir('~/ApenasMeu')
False
For which reason the second does not handle the ~
? 出于何种原因,第二个不处理
~
? How to solve this problem? 如何解决这个问题呢?
To quote the os.path
module documentation : 引用
os.path
模块文档 :
Unlike a unix shell, Python does not do any automatic path expansions.
与unix shell不同,Python不执行任何自动路径扩展。 Functions such as
expanduser()
andexpandvars()
can be invoked explicitly when an application desires shell-like path expansion.当应用程序需要类似shell的路径扩展时,可以显式调用诸如
expanduser()
和expandvars()
函数。 (See also theglob
module.)(另请参见
glob
模块。)
So you can use those functions to perform that expansion: 因此,您可以使用这些功能执行该扩展:
>>> os.path.expanduser('~/ApenasMeu')
'/home/macabeus/ApenasMeu'
>>> os.path.isdir(os.path.expanduser('~/ApenasMeu'))
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.