first commit
This commit is contained in:
commit
f517a71a55
51
add_lines.py
Normal file
51
add_lines.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
# 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)
|
||||||
|
|
||||||
|
|
||||||
|
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\notes' # 修改为你的实际文件夹路径
|
||||||
|
lines_to_add = ['---', 'title:','date:','tags:','categories:','---']
|
||||||
|
insert_lines_in_files(directory, lines_to_add)
|
26
rm_lines.py
Normal file
26
rm_lines.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
def remove_first_n_lines_from_files(directory, n):
|
||||||
|
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:
|
||||||
|
lines = file.readlines()
|
||||||
|
# 检查文件的行数是否多于n
|
||||||
|
if len(lines) > n:
|
||||||
|
# 删除前n行
|
||||||
|
lines = lines[n:]
|
||||||
|
else:
|
||||||
|
# 如果文件行数少于等于n,将其内容清空
|
||||||
|
lines = []
|
||||||
|
|
||||||
|
# 回到文件开头,写入处理后的内容
|
||||||
|
file.seek(0)
|
||||||
|
file.writelines(lines)
|
||||||
|
# 截断文件以删除多余的内容
|
||||||
|
file.truncate()
|
||||||
|
|
||||||
|
# 示例用法
|
||||||
|
directory = r'C:\Users\anenvoy\Desktop\notes' # 修改为你的实际文件夹路径
|
||||||
|
n = 6 # 要删除的行数
|
||||||
|
remove_first_n_lines_from_files(directory, n)
|
Loading…
Reference in New Issue
Block a user