如下记录一次作业:

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

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. 【codeforces 805D】Minimum number of steps

    [题目链接]:http://codeforces.com/contest/805/problem/D [题意] 给你一个字符串; 里面只包括a和b; 让你把里面的"ab"子串全都去 ...

  2. C调用java方法签名

    1.AS2.0 D:\androidMyWork\SmartCam\app\build\intermediates\classes\debug>javap -s com.admin.smartc ...

  3. 使用IO,递归打印目录树

    package chengbaoDemo; import java.io.File; import java.io.IOException; public class TestIOFile { pub ...

  4. asp.net mvc-identity初步学习

    Install-Package Microsoft.AspNet.Identity.EntityFramework Install-Package Microsoft.AspNet.Identity. ...

  5. 工具-VS CODE安装

    在Linux下的安装 1.下载tar.gz文件包, 2.要注意加一条命令,这样在任何目录下就可以使用code .直接启动应用程序了 1 sudo ln -s /path/to/vscode/Code ...

  6. spring容器启动过程理解

    一.一切从手动启动IoC容器开始 ClassPathResource resource = new ClassPathResource("bean.xml"); DefaultLi ...

  7. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps 集成SP和Office App

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps  集成SP和Office App         你能够用两种 ...

  8. 假设让我又一次设计一款Android App

    转载请注明出处: 本文来自aspook的博客:http://blog.csdn.net/ahence/article/details/47154419 开发工具的选择 开发工具我将选用Android  ...

  9. poj 2683 Ohgas&#39; Fortune 利率计算

    水题. 代码: //poj 2683 //sep9 #include <iostream> using namespace std; int main() { int cases; sca ...

  10. 解惑rJava R与Java的快速通道

    阅读导读: 1.什么是RJava? 2.怎样安装RJava? 3.怎样用RJava实现R调用Java? 1. rJava介绍 rJava是一个R语言和Java语言的通信接口.通过底层JNI实现调用,同 ...