python3创建目录

用了这么久python,可能是创建目录的操作写顺手了,一直没注意还有另外的写法。

经常性,需要判断目录是否存在,如果不存在才创建。今天发现一条命令搞定:

os.makedirs("资料/历史资料", exist_ok=True)

而在以前,我习惯性的写得复杂很多,大概是写顺手的原因:

    if not os.path.exists("资料"):
        os.mkdir("资料")
    if not os.path.exists("资料/历史资料"):
        os.mkdir("资料/历史资料")

现在想想,系统内置的一行代码能搞定的事情,我以前偏偏用了四行代码, 真的是笑话啊

发布了113 篇原创文章 · 获赞 24 · 访问量 40万+

猜你喜欢

转载自blog.csdn.net/qq_32394351/article/details/104091814