|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #!/usr/bin/env python3
- #coding:utf-8
- import threading
- import os
- from time import sleep
- def writefile(file):
- os.system('for /l %i in (1,1,20) do (@timeout /t 1 >nul&echo 正在写入新的一行:%i >> '+ file + ')') #调用批处理写入文件
-
- def readfile(filestream):
- filestream.seek(0,2) #移到文件末尾
- while True:
- line = filestream.readline()
- if line:
- yield line
- else:
- sleep(0.5)
-
- def threa(f):
- count = 1
- with open(f) as fs:
- for i in readfile(fs):
- print('\n文件写入了新的内容:',i)
- if count == 20:
- os.system('taskkill /f /pid ' + str(os.getpid()))
- count += 1
- f = r'd:\test.txt'
- if not os.path.exists(f): #文件不存在,创建空文件
- with open(f, 'w') as f:
- pass
- th = []
- th.append(threading.Thread(target = writefile, args = (f,)))
- th.append(threading.Thread(target = threa, args = (f,)))
- for i in range(len(th)):
- th[i].start()
-
- for i in range(len(th)):
- th[i].join()
复制代码
- 文件写入了新的内容: 正在写入新的一行:18
- D:\GIN\py>()
- 文件写入了新的内容: 正在写入新的一行:19
- 文件写入了新的内容: 正在写入新的一行:20
- 成功: 已终止 PID 为 7924 的进程。
- 请按任意键继续. . .
复制代码 |
|