克隆网站后台,做系统用哪个网站好,中国建设银行黑龙江支行官方网站,wordpress 邮箱登录第十一章 文件
打开文件
当前目录中有一个名为beyond.txt的文本文件#xff0c;打开该文件 调用open时#xff0c;原本可以不指定模式#xff0c;因为其默认值就是’r’。
import io
f open(beyond.txt)文件模式
值描述‘r’读取模式#xff08;默认值#xff09;‘w…第十一章 文件
打开文件
当前目录中有一个名为beyond.txt的文本文件打开该文件 调用open时原本可以不指定模式因为其默认值就是’r’。
import io
f open(beyond.txt)文件模式
值描述‘r’读取模式默认值‘w’写入模式‘x’独占写入模式‘a’附加模式‘b’二进制模式与其他模式结合使用‘t’文本模式默认值与其他模式结合使用‘’读写模式与其他模式结合使用
文件的基本方法
读取和写入
在当前路径下创建一个beyond.txt文本文件在该文本文件中写入内容并读取出来。
import io
f open(beyond.txt,w)
f.write(I like beyond band)#结果为18
f.write(I like wsq)#结果为10
f.close()运行结果如下
import io
f open(beyond.txt,r)
f.read(4)#结果为I li
f.read()#结果为ke beyond bandI like wsq首先指定了要读取多少4个字符。接下来读取了文件中余下的全部内容不指定要读取多少个字符。
使用管道重定向输出
在bash等shell中可依次输入多个命令并使用管道将它们链接起来 $ cat beyond.txt | python somescript.py | sort
cat beyond.txt将文件beyond.txt的内容写入到标准输出sys.stdout。
python somescript.py执行Python脚本somescript。这个脚本从其标准输入中读取并将结果写入到标准输出。
sort读取标准输入sys.stdin中的所有文本将各行按字母顺序排序并将结果写入到标准输出。
somescript.py从其sys.stdin中读取数据这些数据是beyond.txt写入的并将结果写入到其sys.stdoutsort将从这里获取数据。
计算sys.stdin中包含多少个单词的简单脚本 somescript.py代码如下
# somescript.py
import systext sys.stdin.read()
words text.split()
wordcount len(words)
print(Wordcount:,wordcount)beyond.txt内容如下
Yellow Skies, I can see the Yellow Skies.
See you again, I see you again
In my dreams, in my dreams, in my dreams, in my dreams.
Morning light, I remember morning light.
Outside my doors, I ll see you no more.
In my dreams, in my dreams, in my dreams, in my dreams
Forever, Forever Ill be forever holding you.
Forever, Forever Ill be forever holding you.
Responsible, Responsible, Responsible, Responsible.
So black and white,
Its become so black and white.cat beyond.txt | python somescript.py
随机存取 所有的文件都可以当成流来进行处理可以在文件中进行移动这称为随机存取。 可使用文件对象的两个方法seek 和 tell。
方法 seek(offset[, whence])将当前位置执行读取或写入的位置移到 offset 和whence 指定的地方。 参数 offset 指定了字节字符数 参数 whence 默认为 io.SEEK_SET0这意味着偏移量是相对于文件开头的偏移量不能为负数。
import io
f open(rE:\Jupyter_workspace\study\python\book\beyond.txt,w)
f.write(beyondhelloword)#结果为15
f.seek(5)#结果为5
f.write(hello beyond)#结果为12
f.read()#结果为beyonhello beyond
#seek(5)此时的指向了d再次进行write操作则会覆盖之后的所有import io
f open(rE:\Jupyter_workspace\study\python\book\beyond.txt)
f.read(3)#结果为bey
f.read(2)#结果为on
f.tell()#结果为5
#这里的tell方法返回的是此时指向的位置读取和写入行
读取一行从当前位置到下一个分行符的文本可使用方法readline。 可不提供任何参数在这种情况下将读取一行并返回它 也可提供一个非负整数指定readline最多可读取多少个字符。
方法writelines接受一个字符串列表实际上可以是任何序列或可迭代对象并将这些字符串都写入到文件或流中。 写入时不会添加换行符因此你必须自行添加。另外没有方法writeline因为可以使用write。
关闭文件
方法close将文件关闭 在python中运行的而结果会存入到缓冲区中有可能没有将结果给你进行立即返回通常程序退出时将自动关闭文件对象并将缓冲器的内容给返回。当然如果不想关闭文件又想将缓冲器的内容及时得到可以使用flush方法。
当然也可以使用try/finally语句并在finally子句中调用close。
import io
f open(rE:\Jupyter_workspace\study\python\book\beyond.txt,w)
try:f.write(like wsq)
finally:f.close()有一条专门为此设计的语句那就是with语句这样是用的最多的方法
import io
with open(rE:\Jupyter_workspace\study\python\book\beyond.txt,w) as f:f.write(like qibao) 上下文管理器 with语句实际上是一个非常通用的结构允许你使用所谓的上下文管理器。 上下文管理器是支持两个方法的对象__enter__和__exit__。 方法__enter__不接受任何参数在进入with语句时被调用其返回值被赋给关键字as后面的变量。 方法__exit__接受三个参数异常类型、异常对象和异常跟踪。它在离开方法时被调用通过前述参数将引发的异常提供给它。如果__exit__返回False将抑制所有的异常.
使用文件的基本方法
beyond.txt内容如下
Yellow Skies, I can see the Yellow Skies.
See you again, I see you again
In my dreams, in my dreams, in my dreams, in my dreams.
Morning light, I remember morning light.
Outside my doors, I ll see you no more.
In my dreams, in my dreams, in my dreams, in my dreams
Forever, Forever Ill be forever holding you.
Forever, Forever Ill be forever holding you.
Responsible, Responsible, Responsible, Responsible.
So black and white,
Its become so black and white.read(n)
import io
f open(rE:\Jupyter_workspace\study\python\book\beyond.txt)
f.read(7)#结果为Yellow
f.read(4)#结果为Skie
f.close()read()
import io
f open(rE:\Jupyter_workspace\study\python\book\beyond.txt)
print(f.read())#结果为Yellow Skies, I can see the Yellow Skies.
See you again, I see you again
In my dreams, in my dreams, in my dreams, in my dreams.
Morning light, I remember morning light.
Outside my doors, I ll see you no more.
In my dreams, in my dreams, in my dreams, in my dreams
Forever, Forever Ill be forever holding you.
Forever, Forever Ill be forever holding you.
Responsible, Responsible, Responsible, Responsible.
So black and white,
Its become so black and white.f.close()readline()
import io
f open(rE:\Jupyter_workspace\study\python\book\beyond.txt)
for i in range(3):print(str(i):f.readline(),end)#结果为0:Yellow Skies, I can see the Yellow Skies.
1:See you again, I see you again
2:In my dreams, in my dreams, in my dreams, in my dreams.f.close()readlines()
import io
import pprint
pprint.pprint(open(rE:\Jupyter_workspace\study\python\book\beyond.txt).readlines())#结果为[Yellow Skies, I can see the Yellow Skies.\n,See you again, I see you again\n,In my dreams, in my dreams, in my dreams, in my dreams.\n,Morning light, I remember morning light.\n,Outside my doors, I ll see you no more.\n,In my dreams, in my dreams, in my dreams, in my dreams\n,Forever, Forever Ill be forever holding you.\n,Forever, Forever Ill be forever holding you.\n,Responsible, Responsible, Responsible, Responsible.\n,So black and white,\n,Its become so black and white.]#这里利用了文件对象将被自动关闭这一事实。write(string)
import io
f open(rE:\Jupyter_workspace\study\python\book\beyond.txt,w)
f.write(I\nlike\nwsq\n)#结果为11
f.close()writelines(list)
import io
f open(rE:\Jupyter_workspace\study\python\book\beyond.txt)
lines f.readlines()
f.close()
lines[1] am\n
f open(rE:\Jupyter_workspace\study\python\book\beyond.txt, w)
f.writelines(lines)
f.close()迭代文件内容 使用read遍历字符
import io
def beyond(string):print(words is:,string)with open(rE:\Jupyter_workspace\study\python\book\beyond.txt) as f:char f.read(1)while char:beyond(char)char f.read(1)words is: I
words is: words is: a
words is: m
words is: words is: w
words is: s
words is: q
words is: 这个程序之所以可行是因为到达文件末尾时方法read将返回一个空字符串
但在此之前返回的字符串都只包含一个字符对应于布尔值True。
只要char为True你就知道还没结束。以不同的方式编写循环
import io
def beyond(string):print(words is:,string)with open(rE:\Jupyter_workspace\study\python\book\beyond.txt) as f:while True:char f.read(1)if not char:breakbeyond(char)
words is: I
words is: words is: a
words is: m
words is: words is: w
words is: s
words is: q
words is: 每次一行 处理文本文件时通常想做的是迭代其中的行而不是每个字符。 方法readline可像迭代字符一样轻松地迭代行。
在while循环中使用readline
import io
def beyond(string):print(words is:,string)with open(rE:\Jupyter_workspace\study\python\book\beyond.txt) as f:while True:line f.readline()if not line:breakbeyond(line)
words is: Iwords is: amwords is: wsq读取所有内容
使用read迭代字符
import io
def beyond(string):print(words is:,string)with open(rE:\Jupyter_workspace\study\python\book\beyond.txt) as f:for char in f.read():beyond(char)words is: I
words is: words is: a
words is: m
words is: words is: w
words is: s
words is: q
words is: 使用readlines迭代行
import io
def beyond(string):print(words is:,string)with open(rE:\Jupyter_workspace\study\python\book\beyond.txt) as f:for line in f.readlines():beyond(line)
words is: Iwords is: amwords is: wsq使用fileinput实现延迟行迭代
有时候需要迭代大型文件中的行此时使用readlines将占用太多内存。 在Python中在可能的情况下应首选for循环。 可使用一种名为延迟行迭代的方法——说它延迟是因为它只读取实际需要的文本部分。
使用fileinput迭代行
import fileinput
import io
def beyond(string):print(words is:,string)for line in fileinput.input(rE:\Jupyter_workspace\study\python\book\beyond.txt):beyond(line)words is: Iwords is: amwords is: wsq文件迭代器
迭代文件
import io
def beyond(string):print(words is:,string)with open(rE:\Jupyter_workspace\study\python\book\beyond.txt) as f:for line in f:beyond(line)
words is: Iwords is: amwords is: wsq在不将文件对象赋给变量的情况下迭代文件
import io
def beyond(string):print(words is:,string)for line in open(rE:\Jupyter_workspace\study\python\book\beyond.txt):beyond(line)words is: Iwords is: amwords is: wsq与其他文件一样sys.stdin也是可迭代的
import sys
import io
def beyond(string):print(words is:,string)for line in sys.stdin:beyond(line)对迭代器做的事情基本上都可对文件做
f open(rE:\Jupyter_workspace\study\python\book\beyond.txt, w)
print(First, line, filef)
print(Second, line, filef)
print(Third, and final, line, filef)
f.close()
lines list(open(rE:\Jupyter_workspace\study\python\book\beyond.txt))
lines#结果为[First line\n, Second line\n, Third and final line\n]first, second, third open(rE:\Jupyter_workspace\study\python\book\beyond.txt)
first#结果为First line\n
second#结果为Second line\n
third#结果为Third and final line\n注意 1使用了print来写入文件这将自动在提供的字符串后面添加换行符。 2对打开的文件进行序列解包从而将每行存储到不同的变量中。 3写入文件后将其关闭以确保数据得以写入磁盘。
小结
概念描述类似于文件的对象类似于文件的对象是支持read和readline可能还有write和writelines等方法的对象。打开和关闭文件要打开文件可使用函数open并向它提供一个文件名。如果要确保即便发生错误时文件也将被关闭可使用with语句。模式和文件类型打开文件时还可指定模式如’r’读取模式或’w’写入模式通过在模式后面加上’b’可将文件作为二进制文件打开并关闭Unicode编码和换行符替换。标准流三个标准流模块sys中的stdin、stdout和stderr都是类似于文件的对象它们实现了UNIX标准I/O机制Windows也提供了这种机制。读取和写入要从文件或类似于文件的对象中读取可使用方法read要执行写入操作可使用方法write。读取和写入行要从文件中读取行可使用readline和readlines要写入行可使用writelines。迭代文件内容迭代文件内容的方法很多其中最常见的是迭代文本文件中的行这可通过简单地对文件本身进行迭代来做到。还有其他与较旧Python版本兼容的方法如使用readlines。
本章介绍的新函数
函数描述open(name, …)打开文件并返回一个文件对象