收藏文章 楼主

修改文件行的内容_python3

版块:python3 脚本程序   类型:普通   作者:小绿叶技术博客   查看:1279   回复:0   获赞:0   时间:2023-12-08 14:04:19


#!/usr/bin/python3
def modify_line(file_path, line_number, new_content):
    with open(file_path, 'r') as file:
        lines = file.readlines()

    if line_number <= len(lines):
        lines[line_number - 1] = new_content

    with open(file_path, 'w') as file:
        file.writelines(lines)

# 示例用法
file_path = 'example.txt'  # 文件路径
line_number = 3  # 要修改的行号
new_content = '新的内容\n'  # 新的行内容

modify_line(file_path, line_number, new_content)











提供企业建站服务,免费网防系统,提交信息登录 http://yundun.ddoss.cn 邮箱: proposal@ddoss.cn 
回复列表
默认   热门   正序   倒序

回复:修改文件行的内容_python3

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息