python使用time库创建时间路径和时间文件

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)

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值