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