1、指定路径、log文件名字
import serial import time import os #对文件、文件夹的操作需要涉及到os模块 DEBUG_LOG = "" TIME = "" TIME = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime()) DEBUG_LOG = "DEBUG_LOG_{}.log".format(TIME) log = open(DEBUG_LOG, 'a') log.write("在当前路径下生成log\r\n") log.close() ##在指的路径下生成log #创建文件夹 folder_name = r'\log-1-' root_directory = r'E:\5G_test\111111111Testcode\learn\1.25' TIME = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime()) try: os.mkdir(root_directory + folder_name) #os.mkdir("file"):创建目录 except OSError: pass #创建文件 # file_path = root_directory + folder_name +TIME+'.log' #这个方法log文件名字、log文件路径都可以在上面定义 file_path = root_directory + '\LOG' +TIME+'.log' #这个方法log文件路径可以在上面定义log文件名字为LOG-TIME.log print(file_path) f = open(file_path, 'a') print(2) f.write("testing\r") f.close() print(3) f = open(file_path, 'a') f.write("+++++\r") f.close() print(4)