如下记录一次作业:

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

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. Android网络编程(十)Retrofit2后篇[注解]

    G相关文章 Android网络编程(一)HTTP协议原理 Android网络编程(二)HttpClient与HttpURLConnection Android网络编程(三)Volley用法全解析 An ...

  2. WinSCP介绍、安装、使用

    前言 如果说XManager通过Xshell.Xftp可以很方便的进行远程管理,那么PuTTY显然不能满足我们的需求,所以这也是今天要介绍的另外一个工具-WinSCP. 简介 WinSCP是一个Win ...

  3. Mybatis分页插件2.0版本号公布

    项目地址:http://git.oschina.net/free/Mybatis_PageHelper 软件介绍:http://www.oschina.net/p/mybatis_pagehelper ...

  4. ubuntu中写一个shell脚本的过程

    gedit hello.sh ,然后输入 #!/bin/bash echo "Hello world!" chmod +x hello.sh ./hello.sh

  5. php+mysql 最简单的留言板

    学完了记得动手操作. 測试地址(未过滤) <html> <body> <head><meta http-equiv="Content-Type&qu ...

  6. Android接口和框架学习

    Android接口和框架学习 缩写: HAL:HardwareAbstraction Layer.硬件抽象层 CTS:CompatibilityTest Suite,兼容性測试套件 Android让你 ...

  7. 《深入理解Android 卷III》第六章 深入理解控件(ViewRoot)系统

    <深入理解Android 卷III>即将公布,作者是张大伟.此书填补了深入理解Android Framework卷中的一个主要空白,即Android Framework中和UI相关的部分. ...

  8. Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP

    E. Pig and Palindromes   Peppa the Pig was walking and walked into the forest. What a strange coinci ...

  9. 【转】详述iOS国际化

    原文网址:http://www.cocoachina.com/ios/20151120/14258.html 在真正将国际化实践前,只知道通过NSLocalizedString方法将相应语言的字符串加 ...

  10. jqueryui slider

    <!doctype html><html lang="en"><head> <meta charset="utf-8" ...