阿兰·佩迪拉,李洪信儿子,魔力小子
检查文件目录是否存在的最优雅方法是什么,如果不存在,如何使用python创建目录?这是我以前使用过的方法:
import os file_path = "/my/directory/filename.txt" directory = os.path.dirname(file_path) try: os.stat(directory) except: os.mkdir(directory) f = file(filename)不知何故,我错过了os.path.exists。现在推荐使用这个方法:
def ensure_dir(file_path): directory = os.path.dirname(file_path) if not os.path.exists(directory): os.makedirs(directory)
所属网站分类: python基础 >
作者:追梦骚年
原文链接:
来源:python黑洞网
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
python求numpy中array按列非零元素的平均值案例
网友评论