一、读取文件,打印第三行时后面加入“徐亚平”
程序如下:
count=0
with open("test",mode="r",encoding="utf8") as f:
for line in f:
if count==2:
line="".join(\[line.strip(),"徐亚平"\])
print(line.strip())
count+=1
Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂
The taste of life was sweet
生命的滋味是甜的
As rain upon my tongue
就如舌尖上的雨露
I teased at life as if it were a foolish game
我戏弄生命 视其为愚蠢的游戏
test
二、修改文件,第三行时后面加入“徐亚平”
因为文件保存在相对应的内存块,故不能在文件前面和文件中间修改,只能在文件尾端追加。
故此时在第三行尾加入“徐亚平”,只能新建文件,删除原先的保留新建的满足题目要求。
程序如下:
count=0
with open("test2",encoding="utf8") as f_read,open("test3",mode="w",encoding="utf8") as f_write:
for line in f_read:
if count==2:
line="".join([line.strip(),"徐亚平\n"])
f_write.write(line)
count+=1
import os
os.rename("test2","test2_bak")
os.rename("test3","test2")
原文件test2
Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂
The taste of life was sweet
生命的滋味是甜的
As rain upon my tongue
就如舌尖上的雨露
I teased at life as if it were a foolish game
我戏弄生命 视其为愚蠢的游戏
test2
三、打印并显示进度条
import sys
for i in range(100):
s="\r%s%% %s"%(i+1,"#"*(i+1))
sys.stdout.write(s)
sys.stdout.flush()
import time
time.sleep(0.5)
print(s)
四、修改haproxy配置文件
1、查
输入:www.oldboy.org
获取当前backend下的所有记录
while True:
m = input("please input url:").strip()
flag = False
l = []
with open("haproxy.conf",encoding="utf8") as f_read: #打开文件并赋值为f_read
for line in f_read: #逐行读取文件haproxy.conf
if line.startswith("backend") and m in line: #如果是“backend”开头并且“m”在此行中
flag=True #不打印不需要,改变标志位值,中止本次操作,重新开始for循环
continue
if line.startswith("backend") and flag: #第二次读到“backend”开头所在的行,结束本层for全部循环
break
if flag: #读到“backend”开头并且“m”后面的行,追加此行到l列表中
l.append(line.strip())
for i in l: #逐条打印l列表中的数据
print(i)
global
log 127.0.0.1 local2
daemon
maxconn 256
log 127.0.0.1 local2 info
defaults
log global
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option dontlognull
listen stats :8888
stats enable
stats uri /admin
stats auth admin:1234
frontend oldboy.org
bind 0.0.0.0:80
option httplog
option httpclose
option forwardfor
log global
acl www hdr_reg(host) -i www.oldboy.org
use_backend www.oldboy.org if www
backend www.oldboy1.org
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33
server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
backend www.oldboy2.org
server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
backend www.oldboy20.org
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33
data=input(): {"backend":"www.oldboy1.org","record":{server:…,weight:…,maxconn:…}}
haproxy配置文件
手机扫一扫
移动阅读更方便
你可能感兴趣的文章