os中关于文件的常用操作
os.makedirs(path) # 创建文件夹
os.path.exists(path) # 查看文件或者文件夹是否存在
os.path.isdir(path) # 查看文件夹是否存在
os.path.isfile(path) # 查看文件是否存在
os.path.getsize(path) # 查看文件内容是否为空,返回文件的字节大小
os.path.basename(path) # 得到path最后的文件名
os.path.join(path1,path2[,path3...]) # 路径组合
os.path.split(path) # 得到目录和文件名
1、在当前项目下创建文件夹
dir = "./data"
output_dir = os.path.join(dir, datetime.datetime.now().strftime(("%Y%m%d_%H%M")))
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print(output_dir) # ./data\20180912_2013
2、在主目录下创建文件夹
dir2 = "~/logs/test1"
output_dir2 = os.path.join(os.path.expanduser(dir2), datetime.datetime.now().strftime(("%Y%m%d_%H%M")))
if not os.path.exists(output_dir2):
os.makedirs(output_dir2)
print(output_dir2) # C:\Users\12735/logs/test1\20180912_2013