在 Python 中判断文件或目录(路径)是否存在是十分方便的,os.path.exists 就可以搞定,但需要进一步判断该路径是否是目录(文件夹),如果不存在,则创建目录。
代码示例:
import os
test_path = './test'
# 判断路径是否存在
if os.path.exists(test_path):
# 判断此路径是否为目录
if os.path.isdir(test_path):
print(te
Python中通过os模块轻松判断文件或目录是否存在,使用os.makedirs创建多级目录,os.rmdir删除目录。利用tempfile模块创建临时文件和目录。本文还推荐了关于NLP中文分词的小册。
在 Python 中判断文件或目录(路径)是否存在是十分方便的,os.path.exists 就可以搞定,但需要进一步判断该路径是否是目录(文件夹),如果不存在,则创建目录。
代码示例:
import os
test_path = './test'
# 判断路径是否存在
if os.path.exists(test_path):
# 判断此路径是否为目录
if os.path.isdir(test_path):
print(te
1246
480
2639

被折叠的 条评论
为什么被折叠?
、判断是否为目录(文件夹)、删除、创建目录(文件夹),创建临时文件tempfile&spm=1001.2101.3001.5002&articleId=104193237&d=1&t=3&u=d9a2dfab20d946359627879a28b1e0f6)