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

程序如下:

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. kb-07线段树-05-区间整体修改查询;(水)

    /* */ #include<iostream> #include<cstring> #include<cstdio> using namespace std; s ...

  2. HDU——2087剪花布条

    剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  3. docker的通俗理解

    自己买了个服务器,前不久搭建好的一个网站,想要再搞一个站点,无奈只能修改端口后,再部署另外一个站点.繁琐的配置运行环境,迁移网站,是否让你感觉到很繁琐?服务器不想用了,想搬迁到另外一台服务器去部署,先 ...

  4. 洛谷 [P2859] 摊位预定

    贪心 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm ...

  5. linux c 正则表达式

    #include <stdio.h> #include <regex.h> #include <mhash.h> int main() { regex_t rgx; ...

  6. 用jquery写的图片懒加载

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  7. 转 PHP中exec、system等函数调用linux命令问题

    PHP中exec.system等函数调用linux命令问题 先小说两句:今天研究了下PHP调用LINUX命令的功能,一开始怎么做都调用不成功,试了好久才终于成功了,所以发出来分享一下.下面我将详细介绍 ...

  8. 开发使用mysql的一些必备知识点整理(四)与python交互

    与python交互 在熟练使用sql语句的基础上,开始使用python语言提供的模块与mysql进行交互 这是我们在工作中大事要做的事 先学会sql是基础,一定要熟练编写sql语句 安装引入模块 安装 ...

  9. EXT.JS6中的model,store,proxy的一些用法

    //one-to-one Ext.define('Address', { extend: 'Ext.data.Model', fields: [ 'address', 'city', 'state', ...

  10. AC日记——最小路径覆盖问题 洛谷 P2764

    题目描述 «问题描述: 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V 中每个顶点恰好在P 的一条路上,则称P是G 的一个路径覆盖.P 中路径可以从V 的任何一个顶点开 ...