第11章 文件

11.1 打开文件

open(file, mode = 'rt')

返回一个文件对象,若文件不存在则返回异常
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'somefile.txt

函数open的参数mode的最常见取值
描述
r读取模式(默认值)
w写入模式(既有内容将被删除)
x独占写入模式,在文件已经存在时引发FileExistsError异常
a附加模式
b二进制模式(与其他模式结合使用)
t文本模式(默认值,与其他模式结合使用)
+读写模式(与其他模式结合使用)

r+和w+均为可读写,但r+不会截断文件,而w+会截断文件


11.2 文件的基本方法

1. 文件的读取和写入

>>>f = open('somefile.txt', 'w')
>>>f.write('Hello, ')
7
>>>f.write('World!')
6
>>>f.close()

>>>f = open('somefile.txt', 'r')
>>>f.read(4)
'Hell'
>>>f.read()
'o, World!'

2. 使用管道重定向输出

在bash等shell中,可依次输入多个命令,并使用管道将它们链接起来,如下所示:
$ cat somefile.txt | python somescript.py | sort

这条管道线包含三个命令:
 cat somefile.txt:将文件somefile.txt的内容写入到标准输出( sys.stdout)。
 python somescript.py:执行Python脚本somescript。这个脚本从其标准输入中读取,并将结果写入到标准输出。
 sort:读取标准输入( sys.stdin)中的所有文本,将各行按字母顺序排序,并将结果写入到标准输出。
但这些管道字符( |)有何作用呢?脚本somescript.py的作用是什么呢?管道将一个命令的标准输出链接到下一个命令的标准输入。很聪明吧?因此可以认为, somescript.py从其sys.stdin中读取数据(这些数据是somefile.txt写入的),并将结果写入到其sys.stdout( sort将从这里获取数据)

3. 随机存取

seek(offset[, whence])将当前位置移到offset和whence指定的地方,offset为字节数,whence可取:
io.SEEK_SET(0)默认,文件开头
io.SEEK_CUR(1)文件当前位置
io.SEEK_END(2)文件结尾

tell()返回文件当前位置

>>>f = open('somefile.txt', 'w')
>>>f.write('01234567890123456789')
20
>>>f.seek(5)
5
>>>f.write('Hello, World!')
13
>>>f.close()
>>>f = open('somefile.txt')
>>>f.read()
'01234Hello, World!89'
>>>f.close()

>>>f = open('somefile.txt')
>>>f.read(3)
'012'
>>>f.read(2)
'34'
>>>f.tell()
5
>>>f.close()

4. 读取和写入行

读取readline()、readlines()

写入writelines()

程序执行前somefile.txt中的内容为

Your mother was a hamster and your
father smelled of elderberries.
Your mother was a hamster and your
father smelled of elderberries.
Your mother was a hamster and your
father smelled of elderberries.
Your mother was a hamster and your
father smelled of elderberries.
Your mother was a hamster and your
father smelled of elderberries.
>>>f = open('somefile.txt')
>>>f.readline(3)
'You'
>>>f.readline()
'r mother was a hamster and your\n'
>>>f.readlines()
['father smelled of elderberries.\n', 'Your mother was a hamster and your\n', 'father smelled of elderberries.\n', 'Your mother was a hamster and your\n', 'father smelled of elderberries.\n', 'Your mother was a hamster and your\n', 'father smelled of elderberries.\n', 'Your mother was a hamster and your\n', 'father smelled of elderberries.']
>>>f.close()
>>>f = open('somefile.txt', 'w')
>>>words = ['aaaaa', 'bbbbbb', 'ccccc', 'ddddddd']
>>>f.writelines(words)
>>>f.close()

 执行完上述代码后文件somefile.txt中的内容

aaaaabbbbbbcccccddddddd

5. 关闭文件

1)close,为确保文件关闭,一般写作

try:
    #将数据写入文件中
finally:
    file.close()

2)专门为此设计的语句

with open('somefile.txt') as somefile:
    do_something(somefile)

 with语句让你能够打开文件并将其赋给一个变量(这里是somefile)。在语句体中,你将数据写入文件(还可能做其他事情)。到达该语句末尾时,将自动关闭文件,即便出现异常亦如此。

如果不写入文件,并非一定要关闭文件


11.3 迭代文件的使用

def process(string):
    print('Processing:', string)

1. 每次一个字符(或字节)

with open(filename) as f:
    char = f.read(1)
    while char:
        process(char)
        char = f.read(1)
with open(filename) as f:
    while True:
        char = f.read(1)
        if not char: break
        process(char)

2. 每次一行

with open(filename) as f:
    while True:
        line = f.readline()
        if not line: break
        process(line)

3. 读取所有内容(文件不太大的情况

#f.read()不带参数,将整个文件读取到一个字符串中
with open(filename) as f:
   for char in f.read():
        process(char)
#readlines方法将文件读取到一个字符串列表中,其中每个字符串是一行
with open(filename) as f:
    for line in f.readlines():
        process(line)

4. 使用fileinput实现延迟行迭代

有时候需要迭代大型文件中的行,此时使用readlines将占用太多内存。当然,可以结合使用while循环和readline,但在Python中,在可能的情况下,应首选for循环,而这里就属于这种情况。可以使用一种名为延迟行迭代的方法——说它延迟是因为它只读取实际需要的文本部分。

import fileinput
for line in fileinput.input(filename):
    process(line)

5. 文本迭代器(最常见的方法,因为文本是可迭代的)

with open(filename) as f:
    for line in f:
        process(line)

如果不对文件进行写操作,那么可以不对文件进行关闭或使用with(即将文件作为上下文管理器)

for line in open(filename):
    process(line)

sys.stdin(即标准输入)也是可以迭代的,因次可以这样做:

import sys
for line in sys.stdin:
    process(line)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MallocLu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值