ja
第十一章 文件和素材
11.1 打开文件
open函数用来打开文件,语法如下:
open(name[,mode[,buffering]])
- f = open(r‘ / home / python / somefile.txt‘)
11.1.1 文件模式
r 读模式
w 写模式
a 追加模式
b 二进制模式(可添加到其他的模式中)
+ 读写模式(可添加到其他的模式中)
在模式参数中使用U参数能够在打开文件时使用通用的换行符支持模式
11.1.2 缓冲
单位是字节,使用flush或者close时更新到磁盘
11.2 基本文件方法
sys.stdin
sys.stdout
sys.stderr 在文件对象中也可以使用
11.2.1 读和写
- >>> f=open(‘somefile.txt‘,‘w‘)
- >>> f.write(‘Hello,‘)
- >>> f.write(‘World!‘)
- >>> f.close()
- >>> f=open(‘somefile.txt‘,‘r‘)
- >>> f.read(4)
- ‘Hell‘
- >>> f.read()
- ‘o,World!‘
11.2.2 管式输出
- # cat somescript.py
- #!/usr/bin/env python
- #coding=utf-8
- # somescript.py
- import sys
- text = sys.stdin.read()
- words = text.split()
- wordcount = len(words)
- print ‘Wordcount:‘, wordcount
- # cat somefile.txt
- Your mother was a hamster and your
- father smelled of elderberries.
- # cat somefile.txt |python somescript.py
- Wordcount: 11
seek(offset[,whence])
tell方法返回当前文件的位置
11.2.3 读写行
file.readline 读取单独的一行
file.readline(5)
file.readlines
file.writelines 读取文件中所有的行并作为列表返回
file.write
没有writeline方法
11.2.4 关闭文件
- try:
- #write data to your file
- fianlly:
- file.close()
- file.fush()
- with open("somefile.txt") as somefile:
- do_someting(somefile)
11.2.5 使用基本文件方法
读
- cat somefile.txt
- Welcome to this file
- There is nothing here except
- This stupid haiku
- >>> f=open(‘/home/mysql/somefile.txt‘)
- >>> f.read(7)
- ‘Welcome‘
- >>> f.read(4)
- ‘ to ‘
- >>> f.close()
- >>> f=open(‘/home/mysql/somefile.txt‘)
- >>>
- >>> print f.read()
- Welcome to this file
- There is nothing here except
- This stupid haiku
- >>> f.close()
- >>> f=open(‘/home/mysql/somefile.txt‘)
- >>> for i in range(3):
- ... print str(i)+":"+f.readline()
- ...
- 0:Welcome to this file
- 1:There is nothing here except
- 2:This stupid haiku
- >> f.close()
- >>> import pprint
- >>> pprint.pprint(open(‘/home/mysql/somefile.txt‘).readlines())
- [‘Welcome to this file\n‘,
- ‘There is nothing here except\n‘,
- ‘This stupid haiku\n‘]
写
- $ cat somefile.txt
- this
- is no
- haiku
- >>> f=open(r‘/home/mysql/somefile.txt‘)
- >>> line=f.readlines()
- >>> f.close()
- >>> line[1]="isn‘t a \n"
- >>> f=open(r‘/home/mysql/somefile.txt‘,‘w‘)
- >>> f.writelines(line)
- >>> f.close()
- $ cat somefile.txt
- this
- isn‘t a
- haiku
11.3 对文件内容进行迭代
- def process(string) : print ‘Processing: ‘,
- string
11.3.1 按字节处理
用read方法对每个字符进行循环
- f = open(filename)
- char = f.read(1)
- while char:
- process(char)
- char = f.read(1)
- f.close()
- f = open(filename)
- while True:
- char = f.read(1)
- if not char: break
- process(char)
- f.close()
11.3.2 按行操作
- f = open(filename)
- while True:
- line = f.readline()
- if not line: break
- process(line)
- f.close()
11.3.3 读取所有内容
如果文件不是很大,可以一次性读取
- f = open(filename)
- for char in f.read():
- process(char)
- f.close()
- f = open(filename)
- for line in f.readlines():
- process(line)
- f.close()
11.3.4 使用fileinput实现懒惰行迭代
fileinput 模块包含了打开文件的函数
- import fileinput
- for line in fileinput.input(filename):
- process(line)
11.3.5 文件迭代器
- f = open(filename)
- for line in f:
- process(line)
- f.close()
- for line in open(filename):
- process(line)
- import sys
- for line in sys.stdin:
- process(line)
可以对文件迭代器执行和普通迭代器相同的操作,list(open(filename))
- >>> f=open(r‘/home/mysql/somefile.txt‘,‘w‘)
- >>> f.write(‘Fist line\n‘)
- >>> f.write(‘Second line\n‘)
- >>> f.write(‘Third line\n‘)
- >>> f.close()
- >>> lines=list(open(‘somefile.txt‘))
- >>> lines
- [‘Fist line\n‘, ‘Second line\n‘, ‘Third line\n‘]
- >>> first,second,third=open(‘somefile.txt‘)
- >>> first
- ‘Fist line\n‘
- >>> second
- ‘Second line\n‘
- >>> third
- ‘Third line\n‘
来源: http://www.bubuko.com/infodetail-2362203.html