Python的os.makedirs无法理解“〜".在我的路上 [英] Python's os.makedirs doesn't understand "~" in my path

查看:139
本文介绍了Python的os.makedirs无法理解“〜".在我的路上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的路径中〜有点问题.

I have a little problem with ~ in my paths.

此代码示例创建一些名为〜/some_dir"的目录,并且不明白我想在自己的主目录中创建some_dir.

This code example creates some directories called "~/some_dir" and do not understand that I wanted to create some_dir in my home directory.

my_dir = "~/some_dir"
if not os.path.exists(my_dir):
    os.makedirs(my_dir)

请注意,这是在基于Linux的系统上.

Note this is on a Linux-based system.

推荐答案

您需要手动扩展波浪符号:

You need to expand the tilde manually:

my_dir = os.path.expanduser('~/some_dir')

这篇关于Python的os.makedirs无法理解“〜".在我的路上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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