![](/img/trans.png)
[英]What is the reason behind getting none when I use print in the function and it is solved when you use return?
[英]For what reason when I use environment variables returns error?
我希望两种情况都返回相同的消息,但只有第一种是正确的
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
出于何种原因,第二个不处理~
? 如何解决这个问题呢?
引用os.path
模块文档 :
与unix shell不同,Python不执行任何自动路径扩展。 当应用程序需要类似shell的路径扩展时,可以显式调用诸如
expanduser()
和expandvars()
函数。 (另请参见glob
模块。)
因此,您可以使用这些功能执行该扩展:
>>> os.path.expanduser('~/ApenasMeu')
'/home/macabeus/ApenasMeu'
>>> os.path.isdir(os.path.expanduser('~/ApenasMeu'))
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.