这篇文章主要讲解了Python文件时间的用法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

一 按时间创建文件
源码
# 截图方式二
# coding=utf-8
import os
import time
# 当前年月日时分秒时间 2020-01-16-10_11_49
picture_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
#当前年月日 2020-01-16
directory_time = time.strftime("%Y-%m-%d", time.localtime(time.time()))
print('当前年月日时分秒时间:'+ picture_time)
print("当前年月日:"+directory_time)
# 获取当前文件目录
print('当前文件目录:'+os.getcwd())
# 获取到当前文件的目录,并检查是否有 directory_time 文件夹,如果不存在则自动新建 directory_time 文件
try:
File_Path = os.getcwd() + '\\' + directory_time + '\\'
print(os.path)
#exists判断文件路径是否存在
if not os.path.exists(File_Path):
os.makedirs(File_Path)
print("目录新建成功:%s" % File_Path)
else:
print("目录已存在!!!")
except BaseException as msg:
print("新建目录失败:%s" % msg)
#切换目录
os.chdir("D:/git")
print('切换后的目录位置:'+os.getcwd())