coding UTF-8 import os ins out open server test txt rb open server tes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# -*- coding: UTF-8 -*-
import os
ins, out = open(r'C:\server\test.txt', 'rb'), \
open(r'C:\server\.test.txt', 'wb')
try:
lines = 0
for line in ins:
lines += 1
needs = lines + 1 - 10
ins.seek(0)
for i, line in enumerate(ins):
if i <= needs - 1:
continue
out.write(line)
out.write('1\r\n')
finally:
ins.close(); out.close()
if os.path.exists(r'C:\server\test.txt') \
and os.path.exists(r'C:\server\.test.txt'):
os.remove(r'C:\server\test.txt') \
and os.rename(r'C:\server\.test.txt', r'C:\server\test.txt')