批处理如何实现删除多个txt文本的最后两行内容

  • A+
所属分类:系统文档

BAT:

  1. @echo off&setlocal enabledelayedexpansion

  2. for %%i in (*.txt) do (

  3. set "f=%%i"

  4. for /f "usebackq delims=" %%j in ("!f!") do set/a n+=1

  5. set/a n-=1

  6. for /f "delims=" %%m in ('"type "!f!"|more& cd. 2^>!f!"') do set/a x+=1&if !x! leq !n! echo;%%m>>!f!

  7. set/a n=0,x=0

  8. )

  9. pause

PYTHON3

  1. import os

  2. import sys

  3.   

  4. try:

  5.     file_name = sys.argv[1]

  6. except Exception as e:

  7.     print('请拖放待处理文件到本脚本')

  8. with open(file_name, 'r') as f:

  9.     lines = f.readlines()

  10. with open(file_name, 'w') as f:

  11.     f.write(''.join(lines[2:]))

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: