![](/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.