Python学习笔记ucas(lecture3)文件处理
2018-02-02 12:43
615 查看
Lecture 3 文件处理
目录
Lecture 3 文件处理目录
1文件的创建打开
2文件的读取
3文件的写入删除
4文件的复制重命名
5文件内容的搜索和替换
6处理二进制文件
7目录的常见操作 os模块ospath模块
数据存储可以用数据库、文件
文件:文本文件、二进制文件
处理文件的模块:os os.path shutil
文本文件:
基本是字符串,如python源码、HTML文件都是文本文件
人容易阅读,程序无法直接阅读,且要比二进制文件大
二进制文件:占据内存小,程序可直接阅读
文件常见操作:打开、读写、复制、删除
1文件的创建、打开
2文件的读取
3文件的写入、删除
4文件的复制、重命名
5文件内容的搜索和替换
6处理二进制文件
7目录的常见操作 os模块、os.path模块
import os import shutil f=open('mll.txt','w') f.write('mll is a student.\n') f.write('mll is a better man.\n') f.close() f=open('mll.txt','a') f.write('haha!\n') f.close() f=open('mll.txt') while True: line=f.readline() if line: print(line) else: break f.close() print("hello world 1") f=open('mll.txt') lines=f.readlines() for line in lines: print(line) f.close() print("hello world 2") f=open('mll.txt') context=f.read() print(context) f.close() print("hello world 3") f=open('mll.txt','w+') context=['hello a\n','hello b\n'] f.writelines(context) f.close() print("hello world 4") open('mll.txt','w') if os.path.exists('mll.txt'): os.remove('mll.txt') print("hello world 5") src=open('hello.txt','w') context=['hello a\n','hello b\n'] src.writelines(context) src.close() src=open('hello.txt','r') dst=open('hello2.txt','w') dst.write(src.read()) src.close() dst.close() print("hello world 6")
相关文章推荐
- Python学习笔记---模块;文件与文本处理
- 廖雪峰python学习笔记10&&11:容错处理与文件读写
- Python学习笔记-初级(四):文件处理
- Python学习笔记ucas(lecture 5)(完结!)类和对象
- Python学习笔记ucas(lecture4)异常处理、GUI、SciPy
- Python学习笔记六:文件处理
- python学习笔记2 Python文件处理
- "Python"学习笔记----简单文件处理
- python学习笔记2 Python文件处理
- python学习笔记2 Python文件处理
- python 学习笔记(10)重构与文件处理
- python学习笔记——文件
- PHP学习笔记:处理文件和操作系统
- php学习笔记(九)文件相关的处理操作
- 学习笔记:文件处理的小技巧
- Python3.3 学习笔记5 - 异常处理
- Python学习笔记——文件对象和操作(2)
- python学习笔记(一)-文件操作
- python学习笔记5-核心类型-元组和文件及其他
- Python的学习(六)----文件和输入输出处理