判断目录是否存在
import os
dirs = '/Users/joseph/work/python/'
if not os.path.exists(dirs):
os.makedirs(dirs)
- 1
- 2
- 3
- 4
- 5
判断文件是否存在
import os
filename = '/Users/joseph/work/python/poem.txt'
if not os.path.exists(filename):
os.system(r"touch {}".format(path))#调用系统命令行来创建文件
- 1
- 2
- 3
- 4
- 5
本文介绍了如何使用Python内置模块os来判断指定目录和文件是否存在的方法。对于不存在的目录,将通过os.makedirs()创建;对于不存在的文件,则通过os.system()调用系统命令创建。
453

被折叠的 条评论
为什么被折叠?



