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

程序如下:

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

python(12)- 文件处理应用Ⅰ的更多相关文章

  1. Python之文件读写

    本节内容: I/O操作概述 文件读写实现原理与操作步骤 文件打开模式 Python文件操作步骤示例 Python文件读取相关方法 文件读写与字符编码 一.I/O操作概述 I/O在计算机中是指Input ...

  2. python 读写文件和设置文件的字符编码

    一. python打开文件代码如下: f = open("d:\test.txt", "w") 说明:第一个参数是文件名称,包括路径:第二个参数是打开的模式mo ...

  3. [Python]读写文件方法

    http://www.cnblogs.com/lovebread/archive/2009/12/24/1631108.html [Python]读写文件方法 http://www.cnblogs.c ...

  4. Python的文件与文件夹操作

    Python的文件与文件夹操作 Python OS模块 1.重命名:os.rename(old, new) 2.删除:os.remove(file) 3.列出目录下的文件 :os.listdir(pa ...

  5. Python 读写文件操作

    python进行文件读写的函数是open或file file_handler = open(filename,,mode) Table mode 模式 描述 r 以读方式打开文件,可读取文件信息. w ...

  6. Python开发 文件操作

    阅读目录 1.读写文件 open()将会返回一个file对象,基本语法: open(filename,mode) filename:是一个包含了访问的文件名称的路径字符串 mode:决定了打开文件的模 ...

  7. python之文件的读写和文件目录以及文件夹的操作实现代码

    这篇文章主要介绍了python之文件的读写和文件目录以及文件夹的操作实现代码,需要的朋友可以参考下 为了安全起见,最好还是给打开的文件对象指定一个名字,这样在完成操作之后可以迅速关闭文件,防止一些无用 ...

  8. 【转】Python之文件读写

    [转]Python之文件读写 本节内容: I/O操作概述 文件读写实现原理与操作步骤 文件打开模式 Python文件操作步骤示例 Python文件读取相关方法 文件读写与字符编码 一.I/O操作概述 ...

  9. Python打包文件夹的方法小结(zip,tar,tar.gz等)

    本文实例讲述了Python打包文件夹的方法.分享给大家供大家参考,具体如下: 一.zip ? 1 2 3 4 5 6 7 8 9 10 11 import os, zipfile #打包目录为zip文 ...

  10. Python OS 文件/目录方法

    Python OS 文件/目录方法 os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os. ...

随机推荐

  1. tzcacm去年训练的好题的AC代码及题解

    A - Tree UVA - 548 You are to determine the value of the leaf node in a given binary tree that is th ...

  2. 记账APP市场分析

    文/欧小慧(简书作者)原文链接:http://www.jianshu.com/p/281fcdce3baa著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 一.市场环境 1.理财记账类用 ...

  3. iOS UICollectionView高级用法(长按自由移动cell)

    iOS 9之后: 示例如下 效果 前言: 看完你可以学到哪些呢? 就是文章标题那么多, 只有那么多. . 手残效果图没弄好. @property (nonatomic, strong) UIColle ...

  4. 性能测试之七--jdbc

    jdbs用任意协议打开都行,具体脚本见下 在vuser_init里面 #include "Ptt_Mysql.h" vuser_init() { lr_load_dll (&quo ...

  5. [解决方案]Window 2008 R2 + IIS7.5 + VS2013 错误代码 0x80070002

    HTTP 错误 404.0 - Not Found 您要找的资源已被删除.已更名或暂时不可用.详细错误信息模块 IIS Web Core通知 MapRequest Handler处理程序 Static ...

  6. 我要好offer之 C++大总结

    0. Google C++编程规范 英文版:http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml 中文版:http://zh-g ...

  7. poj 6243 Dogs and Cages

    Dogs and Cages Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  8. spring执行事务提交后进行一些逻辑操作

    在使用spring事务时,我们通常会把事务内的所有操作当成是一个原子操作.也就是当整个事务内的所有代码都执行完成后, 才会将所有的数据落实到数据库中.这样做有时也会给我们造成麻烦.比如以下场景: 根据 ...

  9. 【CF711C】Coloring Trees(DP)

    题意:给你n个数字,一共有m种,如果某数为0则该数为空,空的地方可以填任意种类数,但每填一个数字都要花费一定的费用, 从头到尾,所有相邻且相同的数字看作一个集合,求使n个数字的集合数为k所需的最小费用 ...

  10. Unity中LoadLevel与LoadLevelAsync的区别

    1.LoadLevel 同步加载 写法:Application.LoadLevel(“name”); 优点:读取场景使用同步的方法就可以,因为是同步方法所以读取的速度是最快的,也不用更新界面,因为同步 ...