在某些时候,波形符 ~ 不再被识别为我的主目录,仅在 Python 中如此。〜仍然可以在终端中使用,所以我不确定发生了什么,但任何有关如何修复它的见解都会为我节省一些打字时间,谢谢!
在 macOS Mojave 上
import os
tilde = '~'
print(os.path.exists(tilde))
os.system("if test -d ~; then echo 'exists'; fi")
Run Code Online (Sandbox Code Playgroud)
输出:
False
exists
Run Code Online (Sandbox Code Playgroud)