You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python删除TXT文件中指定的3proxy用户配置区块?

解决方案

要删除file.txt中specuser相关的配置块,可以通过跟踪行的归属块来实现,以下是修改后的Python脚本:

import fileinput

file_name = 'file.txt'
in_specuser_block = False

for line in fileinput.FileInput(file_name, inplace=1):
    # 检测specuser块的起始分隔线
    if '########################################################################## specuser ##########################################################################' in line:
        in_specuser_block = True
        continue  # 跳过该行输出
    # 如果在specuser块内
    if in_specuser_block:
        # 遇到flush时,结束块标记,且不输出该行
        if 'flush' in line:
            in_specuser_block = False
        continue  # 跳过块内所有行的输出
    # 不在目标块内则正常输出
    print(line, end='')

脚本逻辑说明

  • 用in_specuser_block布尔变量标记当前是否处于需要删除的specuser配置块内
  • 当检测到specuser块的起始分隔线时,开启标记并跳过该行
  • 处于标记状态时,所有行都跳过输出,直到遇到flush行时关闭标记
  • 不在目标块内的行正常输出,保留原有内容

使用说明

  1. 将脚本中的file_name替换为你的实际文件路径(比如/usr/local/etc/3proxy.cfg)
  2. 运行脚本后,目标文件会直接被修改为你需要的结果

内容的提问来源于stack exchange,提问作者Андрей

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 11:01:28