如下记录一次作业:

很显然,我这个应该属于二逼青年版,会在以后更新文艺青年版的答案。

1、模仿sed,一个文件中,用新字符串替换老字符串。

 # file = input("file_name")
# old_str = input("old_str")
# new_str = input("new_str")
def change_str(file, old_str, new_str):
f = open(file, 'r')
lines = f.readlines()
flen = len(lines)
for i in range(flen):
if old_str in lines[i]:
lines[i] = lines[i].replace(old_str, new_str)
open(file, 'w').writelines(lines)
f.close() change_str('aaa', 'A', 'ttttttttttttttttttttttttt')

2、查找、添加、删除特定的内容

 file=open("haproxy_conf","r")
rl=file.readlines()
lens=len(rl)
for i in range(lens):
if "oldboy.org" and "frontend" in rl[i]:
if rl[i-1] is "\n":
print(rl[i].strip())
while True:
i += 1
print(rl[i].strip())
if i == lens-1:
break
 arg = {
'bakend': 'www.oldboy.org',
'record': {
'server': '100.1.7.9',
'weight': 20,
'maxconn': 30
}
}
arg_list=[]
file=open("haproxy_conf","r+")
lines=file.readlines()
print(lines)
for i in arg:
if "bakend" == i:
arg_list.insert(0,i)
arg_list.insert(1,arg[i])
elif "record" == i:
arg_list.append(i)
for key,value in arg[i].items():
if key == "server":
arg_list.insert(3,key)
arg_list.insert(4,value)
for key, value in arg[i].items():
if key == "weight":
arg_list.insert(5,key)
arg_list.insert(6,value)
for key, value in arg[i].items():
if key == "maxconn":
arg_list.insert(7,key)
arg_list.insert(8,value)
arg_list[0]=arg_list[0]+" "+arg_list[1]+"\n"
arg_list.remove(arg_list[1])
arg_list[5]=str(arg_list[5])
arg_list[7]=str(arg_list[7])
arg_list[1]="\t"+arg_list[1]+" "+arg_list[2]+" "+arg_list[3]+" "+arg_list[4]+" "+arg_list[5]+" "+arg_list[6]+" "+arg_list[7]
for m in range(6):
n = 2
arg_list.remove(arg_list[n])
lines.append("\n")
lines.append("\n")
print(lines)
lines = lines +arg_list
print(lines)
file.writelines(lines)
file.close()

[terry笔记]文件操作的更多相关文章

  1. erlang学习笔记(文件操作)

    参考这里和这里了解到的文件操作的模块有很多:kernel下有:file,stdlib下有:filelib,filename,file_sorter.(具体查看官方文档)

  2. python学习笔记:文件操作和集合(转)

    转自:http://www.nnzhp.cn/article/16/ 这篇博客来说一下python对文件的操作. 对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句 ...

  3. day9笔记--文件操作

    文件操作  计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众所周知,应用 ...

  4. python基础学习笔记——文件操作

    文件操作 初始文件操作 使用Python来读写文件是非常简单的操作,我们使用open()函数来打开一个文件,获取到文件句柄,然后通过文件句柄就可以进行各种各样的操作了 根据打开方式的不同能够执行的操作 ...

  5. 03 python学习笔记-文件操作(三)

    本文内容主要包括以下方面: 1. 文件操作基本认识2. 只读(r, rb)3. 只写(w, wb)4. 追加(a, ab)5. r+读写6. w+写读7. a+写读(追加写读)8. 文件的修改 一.文 ...

  6. C学习笔记-文件操作

    文件操作大致分三步 打开文件 读写文件 关闭文件 二进制和文本模式的区别 在windows系统中,文本模式下,文件以"\r\n"代表换行.若以文本模式打开文件,并用fputs等函数 ...

  7. Python学习笔记——文件操作

    python中,一切皆对象.   一.文件操作流程 (1)打开文件,得到一个文件句柄(对象),赋给一个对象: (2)通过文件句柄对文件进行操作: (3)关闭文件. 文件对象f通过open()函数来创建 ...

  8. Python3.5学习笔记-文件操作

    在Python中,操作文件对象使用open函数来创建,下表列出了常用的操作file的函数: 序号 方法及描述 1.file.close() 关闭文件.关闭后文件不能再进行读写操作. 2.file.fl ...

  9. c语言学习笔记 - 文件操作

    #include <stdio.h>#include <time.h> int main(void){ time_t t;               //类似于size_t那 ...

随机推荐

  1. 使用SeaJS,require加载Jquery的时候总是为null

    这个问题困扰了我两天,使用别人的例子.官网down下来的example都没有问题.但是放到自己项目里就 var $=require("jquery") 为null. 后来发现,jq ...

  2. telnet允许root用户登录

    默认情况下,linux不允许root用户以telnet方式登录linux主机,若要允许root用户登录,可采取以下3种方法之一:    1.修改login文件 redhat中对于远程登录的限制体现在/ ...

  3. 使用joomla通过CSV文件上传数据存入数据库并使用JavaScript验证码是否符合规则

    1,实现效果截图 2,A.php上传CSV文件表单 2-1:html结构使用jqeury.form.min.js表单框架异步提交 <div class="uploadFile bord ...

  4. MFC 与Excel文件的交互操作

    假日快要结束了.带着沉重的心情写下之前关于MFC与Excel文件交互的总结. 因为VS的版本号不同可能在操作上有些差异.所以在此指明下本篇文章的project环境为VS2013,也建议大家用最新的. ...

  5. Nginx系列(三)--管理进程、多工作进程设计

    Nginx由一个master进程和多个worker进程组成,但master进程或者worker进程中并不会再创建线程. 一.master进程和worker进程的作用 master进程 不须要处理网络事 ...

  6. log4j日志存储到数据库

    一.前提条件 系统必须是使用LOG4J进行日志管理,否则方法无效. 系统必须包含commons-logging-xxx.jar,log4j-xxx.jar这两个JAR包,XXX为版本号. 二.操作步骤 ...

  7. CodeForcess--609B--The Best Gift(模拟水题)

    The Best Gift Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submi ...

  8. db file sequential read等待事件 --转载

    db file sequential read db file sequential read等待事件有3个参数:file#,first block#,和block数量.在10g中,这等待事件受到用户 ...

  9. POJ 3180 Tarjan

    题意:找强连通中点数大于2的强连通分量个数 思路:Tarjan // By SiriusRen #include <cstdio> #include <algorithm> u ...

  10. 在eclipse环境下使用maven install 命令碰到native2ascii-utf8问题解决方案

    报错语句:Failed to execute goal org.codehaus.mojo:native2ascii-maven-plugin:1.0-beta-1:native2ascii (nat ...