Python 文件基本操作
来源:互联网 发布:手机版淘宝怎么开店 编辑:程序博客网 时间:2024/06/06 16:55
# -*- coding:utf-8 -*-import os'''open函数open(name,mode)-name 文件名-mode 访问模式 r 读方式 w 写方式 a 以追加模式打开(必要时创建新文件) r+,w+,a+ 读写模式'''#文件属性try:wf0 = open('file.txt')print wf0.encoding #文件编码方式print wf0.mode #访问模式print wf0.name #文件名print wf0.closed #是否关闭finally:wf0.close()#读取所有try:f0 = open('file.txt','r')txt0 = f0.read()print txt0finally:f0.close()#按长度读取try:f1 = open('file.txt')while True:tmp = f1.read(6)if not tmp:breakprint tmpfinally:f1.close()#逐行读取2print '------------------------------'try:f1 = open('file.txt');for line in f1.readlines():print line;except Exception, e:print e;finally:if f1:f1.close();print '------------------------------'#逐行读取1try:f2 = open('file.txt')while True:txt2 = f2.readline()if not txt2:breakprint "%d:%s" % (len(txt2),txt2)finally:f2.close()#写入内容try:wf0 = open('file.txt','a')wf0.write('append')finally:wf0.close()#写入多行内容strs = ['1','2\n','3']try:wf0 = open('file.txt','a')wf0.writelines(strs)finally:wf0.close()
0 0
- Python文件基本操作
- python基本文件操作
- Python 文件基本操作
- Python 文件基本操作
- Python文件基本操作
- Python基本文件操作
- python读写文件基本操作
- Python基础--基本文件操作
- Python 中的基本文件操作
- 小白学Python(二) 基本文件操作
- python对文件的基本操作
- python文件路径的基本操作
- Python基础--基本文件操作二
- Python学习笔记之:文件基本操作
- Python零碎知识(3):对文件的基本操作
- python对文件的几大基本操作
- Python基本语法_文件操作_读写函数详解
- Python 基本类型操作
- Android Studio2.3.1查看gradle版本
- 路由器逆向分析------QEMU的基本使用方法(MIPS)
- LeetCode037 Sudoku Solver
- C#多线程编程
- springmvc时间类型提交转换的问题
- Python 文件基本操作
- matlab中的Sobel算子C程序源码
- Liunx下的Oracle备份
- [c++基础] 四种类型转换和隐式类型转换
- LeetCode039 Combination Sum
- java读取文件内容几种方式
- LeetCode038 Count and Say
- 硬币找零
- PATl两个有序链表序列的合并