Python数据分析script必备知识(四)

Python数据分析script必备知识(四)

1.保留最近N天的日志

1.批量创建多个文件

首先,为演示方便,在指定文件夹目录下用下面代码批量新建多个log文件

"""
批量创建多个文件
"""
import os
file_path = os.path.join(os.getcwd(),'LOG')
# 如果不存在改路径,创建;如果存在,不做处理
if not os.path.exists(file_path):
    os.makedirs(file_path,exist_ok=True)
for i in range(1,31):
    # 这里的./指代的是当前文件夹,i表示文件的名称,a表示没有该文件就新建
    # 创建多个txt文件
    # f = open('./2023-05-0%s'% i + '.txt','a')
    # 创建多个log文件
    f = open('./LOG/2023-05-0%s' % i + '.log', 'a')
    f.write('测试')
    f.close()


如图所示:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2euTNChG-1685887694588)(E:/data_analysis/data_analysis_combat/Typora_image/051.png)]

猜你喜欢

转载自blog.csdn.net/m0_57021623/article/details/131037968