Python中的file(上)
来源:互联网 发布:知乎怎么查看我的收藏 编辑:程序博客网 时间:2024/06/08 06:09
codecs 这个模块用于解决文件乱码问题
open(filename,mode)
mode有几个参数需要学习:
r : 读
w: 写
b: 二进制
a:追加
#打开文件需要几步#1.open文件#2.文件操作(读或写)#3.关闭文件
------------------------------------------------------------------------------------
1、file读文件
import codecsf = codecs.open('1.txt')print(f.read())f.close
2、file写文件
f = codecs.open('2.txt', 'w')f.write('hello world\n')f.close()
f = codecs.open('2.txt', 'a')f.write('welcome!\n')f.close()
在此基础上可以进行一些扩展,比如替换:
f = codecs.open('2.txt')x = f.read()y = x.replace('welcome', 'haha')print (y)hello world
haha!
阅读全文
0 0
- Python中的file(上)
- python中的输入输出(file)
- Python中的file(下)
- Python 中的类(上)
- Python中的类(上)
- Python 中的类(上)
- Python 中的类(上)
- 初探python中的file类型
- Java中的文件和目录管理(上)---File类
- Python中的列表(上)
- 有关 Python 中的 File.readlines(sizehint) 函数
- python文件管理中的file、seek、truncate
- python文件管理中的file、seek、truncate
- Python 中的 file.flush() 与 os.fsync()
- python file
- Python---file
- python file
- Python file
- 给字符串对象定义一个repeat功能。当传入一个整数n时,它会返回重复n次字符串的结果。
- dts从uboot加载到kernel使用案例的分析
- db2
- 1.桶排序
- windows编程一日一练(2)
- Python中的file(上)
- 74LS273 八D型触发器 功能介绍
- Centos搭建ceph+++十一、测试存入/检出对象数据
- 完整禁止查看网页源代码的写法、onload加载两个函数
- spring boot redis缓存JedisPool
- javaWeb购物商城设计---总体设计
- Nginx访问静态文件配置
- python nc basemap
- 搭建Kali Linux 基础环境