txt文本单次删除第一行
# txt文本单次删除第一行
with open('test.txt', mode='r', encoding='utf-8') as f:
line = f.readlines() # 读取文件
try:
line = line[1:] # 只读取第一行之后的内容
f = open('test.txt', mode='w', encoding='utf-8') # 以写入的形式打开txt文件
f.writelines(line) # 将修改后的文本内容写入
f.close() # 关闭文件
except:
pass
txt文本循环删除第一行,直到删完整个文本
# txt文本循环删除第一行,直到删完整个文本
with open('test.txt', mode='r', encoding='utf-8') as f:
line = f.readlines() # 读取文件
N = len(line) # 获取txt文件的行数
for i in range(0,N):
try:
line = line[1:] # 只读取第一行之后的内容
f = open('test.txt', mode='w', encoding='utf-8') # 以写入的形式打开txt文件
f.writelines(line) # 将修改后的文本内容写入
f.close() # 关闭文件
except:
pass
需要批量删除文件夹(包含子文件夹)下所有txt文本第一行数据的请参考:
https://blog.csdn/weixin_46269351/article/details/114268100
更多推荐
python删除txt文本文件第一行数据
发布评论