【Python学习笔记】os.path模块

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值