Python data analysis script necessary knowledge (4)

Python data analysis script necessary knowledge (4)

1. Keep the logs of the last N days

1. Create multiple files in batches

First of all, for the convenience of demonstration, use the following code to create multiple log files in batches in the specified folder directory

"""
批量创建多个文件
"""
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()


as the picture shows:

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-2euTNChG-1685887694588)(E:/data_analysis/data_analysis_combat/Typora_image/051.png)]

Guess you like

Origin blog.csdn.net/m0_57021623/article/details/131037968