一、读取文件,打印第三行时后面加入“徐亚平”

程序如下:

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配置文件

最新文章

  1. Longest Substring Without Repeating Characters
  2. mongodb分片配置
  3. [转]runtime 消息机制
  4. split 分割 字符串(分隔符如:* ^ : | , .) 及注意点
  5. Nutz中那些好用的工具类
  6. hdu 2010 - 水仙花数
  7. XML工程配置文件的读写
  8. HDU--1060
  9. 72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)
  10. 【爆料】-《布莱顿大学毕业证书》Brighton一模一样原件
  11. DSAPI 导出EXEDLL函数到字符串
  12. C++_day8pm_多态
  13. Information Retrieval 倒排索引 学习笔记
  14. 图->存储结构->邻接多重表
  15. 学习docker后的个人理解
  16. ANSYS渡槽槽身动水压力的施加(1)——矩形渡槽
  17. Helm 入门安装指南
  18. PHPCMS增加SEO字段调用
  19. 解决vsftpd乱码
  20. maven下拉项目不能同步

热门文章

  1. 集合篇 —— Collection(1):JDK 中的重复实现问题
  2. outline:0与outline:none区别
  3. [USACO08DEC] 秘密消息Secret Message (Trie树)
  4. 【前端学习笔记】2015-09-06 ~~~~ setAttribute()、slice()
  5. python -- DNS处理模块dnspython
  6. 定时任务管理之python篇celery使用
  7. codeforces 446C DZY Loves Fibonacci Numbers 数论+线段树成段更新
  8. linux内核中打印栈回溯信息 - dump_stack()函数分析【转】
  9. [leetcode]84.Largest Rectangle in Histogram ,O(n)解法剖析
  10. LeetCode OJ——Binary Tree Inorder Traversal