113 lines
4.8 KiB
Python
113 lines
4.8 KiB
Python
############################################################################################################
|
|
# v0.0.1
|
|
# import os
|
|
|
|
# def add_lines_to_files(directory, lines_to_add):
|
|
# for filename in os.listdir(directory):
|
|
# filepath = os.path.join(directory, filename)
|
|
# if os.path.isfile(filepath):
|
|
# with open(filepath, 'r+', encoding='utf-8') as file:
|
|
# content = file.read()
|
|
# file.seek(0, 0)
|
|
# file.write('\n'.join(lines_to_add) + '\n' + content)
|
|
|
|
# # 示例用法
|
|
# directory = r'C:\Users\anenvoy\Desktop\notes'
|
|
# lines_to_add = ['---', 'title:','date:','tags:','categories:','---']
|
|
|
|
# add_lines_to_files(directory, lines_to_add)
|
|
|
|
#############################################################################################################
|
|
# v0.0.2
|
|
# import os
|
|
|
|
# def insert_lines_in_files(directory, lines_to_add):
|
|
# for filename in os.listdir(directory):
|
|
# filepath = os.path.join(directory, filename)
|
|
# if os.path.isfile(filepath):
|
|
# with open(filepath, 'r+', encoding='utf-8') as file:
|
|
# content = file.readlines()
|
|
# first_line = content[0].strip() if content else '' # 获取原文的第一行,并去掉空白字符
|
|
|
|
# # 去掉文件名的后缀名
|
|
# file_name_without_ext = os.path.splitext(filename)[0]
|
|
|
|
# # 生成新的内容
|
|
# if len(lines_to_add) >= 2:
|
|
# # new_lines = lines_to_add[:1] + [filename] + [f"{lines_to_add[2]} {first_line}"] + lines_to_add[3:]
|
|
# new_lines = lines_to_add[:1] + [f"{lines_to_add[1]} {file_name_without_ext}"] + lines_to_add[2:]
|
|
# else:
|
|
# new_lines = [filename, f"{first_line}"] # 如果 lines_to_add 为空或长度不足,处理情况
|
|
|
|
# # 将文件内容与新行结合
|
|
# new_content = '\n'.join(new_lines) + '\n' + ''.join(content)
|
|
|
|
# # 回到文件开头,写入新内容
|
|
# file.seek(0)
|
|
# file.write(new_content)
|
|
# # 截断文件以删除多余的内容
|
|
# file.truncate()
|
|
|
|
# # 示例用法
|
|
# directory = r'C:\Users\anenvoy\Desktop\Payment_System' # 修改为你的实际文件夹路径
|
|
# lines_to_add = ['---', 'title: ','date: ','cover: /static/img/dccf965f.jpg','tags: eviwbh','categories: eviwbh','---']
|
|
# insert_lines_in_files(directory, lines_to_add)
|
|
|
|
|
|
#############################################################################################################
|
|
# v0.0.3
|
|
import os
|
|
import random
|
|
from datetime import datetime, timedelta
|
|
|
|
def random_date(start, end):
|
|
"""
|
|
生成指定范围内的随机日期时间
|
|
:param start: 起始时间 (datetime)
|
|
:param end: 结束时间 (datetime)
|
|
:return: 随机生成的时间 (str),格式为 'YYYY-MM-DD HH:MM:SS'
|
|
"""
|
|
delta = end - start
|
|
random_seconds = random.randint(0, int(delta.total_seconds()))
|
|
random_datetime = start + timedelta(seconds=random_seconds)
|
|
return random_datetime.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
def insert_lines_in_files(directory, lines_to_add):
|
|
# 指定时间范围
|
|
start_date = datetime(2019, 1, 1)
|
|
end_date = datetime(2024, 9, 17)
|
|
|
|
for filename in os.listdir(directory):
|
|
filepath = os.path.join(directory, filename)
|
|
if os.path.isfile(filepath):
|
|
with open(filepath, 'r+', encoding='utf-8') as file:
|
|
content = file.readlines()
|
|
first_line = content[0].strip() if content else '' # 获取原文的第一行,并去掉空白字符
|
|
|
|
# 去掉文件名的后缀名
|
|
file_name_without_ext = os.path.splitext(filename)[0]
|
|
|
|
# 生成随机时间
|
|
random_time = random_date(start_date, end_date)
|
|
lines_to_add[2] = f"date: {random_time}"
|
|
|
|
# 生成新的内容
|
|
if len(lines_to_add) >= 2:
|
|
new_lines = lines_to_add[:1] + [f"{lines_to_add[1]} {file_name_without_ext}"] + lines_to_add[2:]
|
|
else:
|
|
new_lines = [filename, f"{first_line}"] # 如果 lines_to_add 为空或长度不足,处理情况
|
|
|
|
# 将文件内容与新行结合
|
|
new_content = '\n'.join(new_lines) + '\n' + ''.join(content)
|
|
|
|
# 回到文件开头,写入新内容
|
|
file.seek(0)
|
|
file.write(new_content)
|
|
# 截断文件以删除多余的内容
|
|
file.truncate()
|
|
|
|
# 示例用法
|
|
directory = r'C:\Users\anenvoy\Desktop\Payment_System' # 修改为你的实际文件夹路径
|
|
lines_to_add = ['---', 'title: ','date: ','cover: /static/img/dccf965f.jpg','tags: eviwbh','categories: eviwbh','---']
|
|
insert_lines_in_files(directory, lines_to_add)
|