Python找不到使用首页符号的目录〜 [英] Python doesn't find a directory using the home sign ~

查看:98
本文介绍了Python找不到使用首页符号的目录〜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小的Python脚本,用于将目录保存在当前用户下的副目录中. 我在Mac上运行,但是生产是Ubuntu

I have created a small Python script to save directories in a side directory, under the current user. I am running on Mac but production is Ubuntu

我的问题是它无法识别带有首页符号~

My problem is the it doesn't manage to identify the dir with the home sign ~

>>> os.path.exists('/Users/partuck/cache_dir/bla')
True
>>> os.path.exists('~/cache_dir/bla')
False
>>> os.system('echo "$USER"')
partuck
0

推荐答案

从文档(此处为

From the docs (here, from the glob module):

(对于波浪号和shell变量扩展,请使用 os.path.expanduser() os.path.expandvars() .)

(For tilde and shell variable expansion, use os.path.expanduser() and os.path.expandvars().)

您要os.path.expanduser().

这篇关于Python找不到使用首页符号的目录〜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆