import time import os class FilePath(object): def __init__(self): # 获得当前系统时间的字符串 self.local_time = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time())) print(self.local_time) print(type(self.local_time)) # 获取年字符串 self.year = self.local_time.split("-")[0] print(self.year) # 获取月字符串 self.month = self.local_time.split("-")[1] # 获取时字符串 self.day = self.local_time.split("-")[2] # 获取时,分,秒字符串 self.hour = self.local_time.split("-")[3] self.minute = self.local_time.split("-")[4] self.second = self.local_time.split("-")[5] # 判断report文件夹是否存在不存在则创建 base_path = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0] print(base_path) if not os.path.exists(base_path + "/report"): os.mkdir(base_path + "/report") self.fileYear = '../report/' + '/' + self.year self.fileMonth = self.fileYear + '/' + self.month self.fileDay = self.fileMonth + '/' + self.day self.file_name = self.year+self.month+self.day+self.hour+self.minute+self.second def get_file_name(self): """创建时间文件夹路径""" if not os.path.exists(self.fileDay): if not os.path.exists(self.fileYear): os.mkdir(self.fileYear) os.mkdir(self.fileMonth) os.mkdir(self.fileDay) else: if not os.path.exists(self.fileMonth): os.mkdir(self.fileMonth) os.mkdir(self.fileDay) else: if not os.path.exists(self.fileDay): os.mkdir(self.fileDay) return self.fileDay if __name__ == '__main__': fp = FilePath() fp.get_file_name() print(fp.file_name)
03-15
508