[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那 ...
随机推荐
- 如何在 VMware 上安装 CentOS 6.8
一.下载 CentOS 6.8 64位 链接:https://pan.baidu.com/s/15qmWGar2m0WzsWxDk4tSSg 密码:wqra 二.安装步骤 1.新建虚拟机 2.自定义 ...
- HDU2188 选拔志愿者
/* HDU2188 选拔志愿者 http://acm.hdu.edu.cn/showproblem.php?pid=2188 博弈论 巴什博奕 巴什博奕要注意n<=m时是必胜态 * * * * ...
- [SharePoint]2013装过WindowsServerAppFabricSetup_x64_6.1导致安装不能继续
还是不要自己手动安装2013支持组件.让他自己慢慢下吧. 浪费一个星期的时间. 特此纪念! 令解http://www.cnblogs.com/jianyus/p/3287625.html
- C专家编程之为什么C语言把数组形參当做指针:数组/指针实參
#include<stdio.h> void print_array_test(char ca[]) { printf("ca : %s\n",ca); printf( ...
- System v shm的key
shmget函数用于创建或打开一个共享内存区对象,shmget成功调用会返回一个共享内存区的标识符,供其它的共享内存区操作函数使用. key:用于创建共享内存区的键值,这个在前面其他System IP ...
- rails undefined method error_messages
rails undefined method error_messages 学习了:http://stackoverflow.com/questions/10002140/use-error-mess ...
- Boost.Asio c++ 网络编程翻译(16)
TCP异步服务端 核心功能和同步服务端的功能类似,例如以下: class talk_to_client : public boost::enable_shared_from_this<talk_ ...
- Android面试题集
前几天整理了Java面试题集合,今天再来整理下Android相关的面试题集合.假设你希望能得到最新的消息,能够关注https://github.com/closedevice/interview-ab ...
- c# 无法加载xxx.dll 找不到指定的模块(如何指定文件夹)
如果直接放在项目运行目录,例如bin/debug可以直接加载,但是这样比较乱. 如果在放debug里面的一个文件夹里面,有可能会报错“无法加载xxx.dll 找不到指定的模块”. 如果路径写成这样就会 ...
- Cocos2d-x3.0 RenderTexture(三)
.h #include "cocos2d.h" #include "cocos-ext.h" #include "ui/CocosGUI.h" ...