[terry笔记]文件操作
如下记录一次作业:
很显然,我这个应该属于二逼青年版,会在以后更新文艺青年版的答案。
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笔记]文件操作的更多相关文章
- erlang学习笔记(文件操作)
参考这里和这里了解到的文件操作的模块有很多:kernel下有:file,stdlib下有:filelib,filename,file_sorter.(具体查看官方文档)
- python学习笔记:文件操作和集合(转)
转自:http://www.nnzhp.cn/article/16/ 这篇博客来说一下python对文件的操作. 对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句 ...
- day9笔记--文件操作
文件操作 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众所周知,应用 ...
- python基础学习笔记——文件操作
文件操作 初始文件操作 使用Python来读写文件是非常简单的操作,我们使用open()函数来打开一个文件,获取到文件句柄,然后通过文件句柄就可以进行各种各样的操作了 根据打开方式的不同能够执行的操作 ...
- 03 python学习笔记-文件操作(三)
本文内容主要包括以下方面: 1. 文件操作基本认识2. 只读(r, rb)3. 只写(w, wb)4. 追加(a, ab)5. r+读写6. w+写读7. a+写读(追加写读)8. 文件的修改 一.文 ...
- C学习笔记-文件操作
文件操作大致分三步 打开文件 读写文件 关闭文件 二进制和文本模式的区别 在windows系统中,文本模式下,文件以"\r\n"代表换行.若以文本模式打开文件,并用fputs等函数 ...
- Python学习笔记——文件操作
python中,一切皆对象. 一.文件操作流程 (1)打开文件,得到一个文件句柄(对象),赋给一个对象: (2)通过文件句柄对文件进行操作: (3)关闭文件. 文件对象f通过open()函数来创建 ...
- Python3.5学习笔记-文件操作
在Python中,操作文件对象使用open函数来创建,下表列出了常用的操作file的函数: 序号 方法及描述 1.file.close() 关闭文件.关闭后文件不能再进行读写操作. 2.file.fl ...
- c语言学习笔记 - 文件操作
#include <stdio.h>#include <time.h> int main(void){ time_t t; //类似于size_t那 ...
随机推荐
- python 在爬虫中timeout设置超时有什么作用
是为了防止url不可访问,或者响应速度太慢而造成的时间浪费. 比如,你要爬取1000个网站,如果有100个需要30s才能返回数据,你等待他们返回的话就需要3000s了,如果你设置10s超时,那么就能知 ...
- hdu 1163
别人的代码开始自己不知道什么数论解法: ab*ab=(a*10+b)(a*10+b)=a^2*100+2ab*10+b^2 所以the root digital=(a+b)*(a+b): 而数论中的定 ...
- Python标准库:内置函数callable(object)
假设对象object參数是能够调用的对象,就返回True.否则返回False.只是要注意的是,当一个对象是能够调用的.并不表示调用该对象时运行一定成功,但不可调用的对象去调用时一定不会成功.假设类对象 ...
- pthread_create()创建线程时传入多个參数
因为接口仅仅定义了一个入參void *arg int pthread_create(pthread_t *tidp,const pthread_attr_t *attr, (void*)(*start ...
- hdu(2846)Repository
Problem Description When you go shopping, you can search in repository for avalible merchandises by ...
- DB-MySQL:MySQL 教程
ylbtech-DB-MySQL:MySQL 教程 1.返回顶部 1. MySQL 教程 MySQL 是最流行的关系型数据库管理系统,在WEB应用方面 MySQL 是最好的RDBMS(Relation ...
- 杂项-软件: VBA(Visual Basic for Applications)
ylbtech-杂项-软件: VBA(Visual Basic for Applications) VBA (Visual Basic宏语言) Visual Basic for Application ...
- [MySQL] 查询一段时间记录
24小时内记录(即86400秒) $sql="SELECT video_id,count(id)as n FROM `rec_down` WHERE UNIX_TIMESTAMP(NOW() ...
- centos7安装mysql(转载)
Centos7安装并配置mysql5.6完美教程 Centos7将默认数据库mysql替换成了Mariadb,对于我们这些还想使用mysql的开发人员来说并不是一个好消息.然而,网上关于Linux安装 ...
- Bata版本
一.团队成员 1)冯鹏(组长) 201731062617 2)鲜泽 201731062612 3)李家豪 201731062614 4)郭经伟 201731062615 5)程前勇 2017310 ...