简体   繁体   English

由于什么原因,当我使用环境变量时会返回错误?

[英]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() and expandvars() can be invoked explicitly when an application desires shell-like path expansion. 当应用程序需要类似shell的路径扩展时,可以显式调用诸如expanduser()expandvars()函数。 (See also the glob 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.

相关问题 当我在函数中使用print时得到none的原因是什么,当你使用return时它解决了? - What is the reason behind getting none when I use print in the function and it is solved when you use return? 当我使用以下 python 代码时,机器 epsilon 得到两个不同的数量,这是什么原因? - I get two different amount for machine epsilon when I use below python codes, what is the reason? 当我启动 Windows 命令提示符时,我做了什么导致环境变量发生变化? - What did I do to cause environment variables to change when I start the Windows command prompt? 此错误的原因是什么,我该如何解决? - What is the reason for this error and how can I fix it? 将 tkinter 用于数学应用程序时出现此错误的原因是什么 - What is the reason of this error I'm getting when using tkinter for a math app 当我将+ =放入字典而不是=时出现错误的原因 - The reason of error when I put += in dictionary but not in = 执行中出现此错误的原因是什么? - What is the reason for this error in execution? 在CircleCI中使用环境变量 - use environment variables in CircleCI 声明cpdef枚举时,此Cython编译错误的原因是什么? - What is the reason for this Cython compile error when declaring a cpdef enum? 在python3中将数据插入mysql时出错的原因是什么? - what is the reason for the error when inserting data into mysql in python3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM