
(1) 从文件中读取数据
首先编写 pi.txt
3.1415926535
8979323846
2643383279
然后编写 file_reader.py
# 当前运行的是file_reader.py,因此Python在file_reader.py所在的目录中查找pi.txt。
# 关键字with在不再需要访问文件后将其关闭。
with open('pi.txt') as file_object:
contents = file_object.read()
print(contents.rstrip())
输出吧,皮卡丘~
PS D:\vscode> & C:/Users/Administrator/AppData/Local/Programs/Python/Python37/python.exe d:/vscode/file_reader.py
3.1415926535
8979323846
2643383279
PS D:\vscode>
(2) 文件路径
例如,程序存储在python_work文件夹中,而python_work中,有text_files文件夹。
在Linux和OS X中,可以这样编写文件路径:
# 这行代码让Python到python_work文件夹下的text_files文件夹中去查找指定的.txt文件
with open('text_files/filename.txt') as file_object:
在Windows系统中,在文件路径中使用反斜杠(\)而不是斜杠(/):
with open('text_files\filename.txt') as file_object:
当然,还可以将文件在计算机中的准确位置告诉Python,这样就不用关心当前运行的程序存储在
什么地方了,这称为绝对文件路径。
在Linux和OS X中,绝对路径类似于下面这样:
file_path = '/home/pm/other_files/text_files/filename.txt'
with open(file_path) as file_object:
在Windows系统中,绝对路径类似于下面这样:
file_path = 'C:\Users\pm\other_files\text_files\filename.txt'
with open(file_path) as file_object:
(3) 逐行读取
filename = 'pi.txt'
with open(filename) as file_object:
for line in file_object:
print(line)
输出:
PS D:\vscode> & C:/Users/Administrator/AppData/Local/Programs/Python/Python37/python.exe d:/vscode/hello.py
3.1415926535
8979323846
2643383279
PS D:\vscode>
为何会出现这些空白行呢?因为在这个文件中,每行的末尾都有一个看不见的换行符,而print语句也会加上一个换行符,因此每行末尾都有两个换行符:一个来自文件,另一个来自print语句。要消除多余的空白行,可在print语句中使用rstrip()。
(4) 创建一个包含文件各行内容的列表
filename = 'pi.txt'
with open(filename) as file_object:
# 方法readlines()从文件中读取每一行,并将其存储在一个列表中
lines = file_object.readlines()
# 在with代码块外,依然可以使用lines这个变量。
for line in lines:
print(line.rstrip())
输出:
PS D:\vscode> & C:/Users/Administrator/AppData/Local/Programs/Python/Python37/python.exe d:/vscode/hello.py
3.1415926535
8979323846
2643383279
PS D:\vscode>
(5) 使用文件的内容
filename = 'pi.txt'
with open(filename) as file_object:
lines = file_object.readlines()
pi_string = ''
for line in lines:
pi_string += line.strip()
print(pi_string)
print(len(pi_string))
输出:
3.141592653589793238462643383279
32
其中,strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。注意该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。
(6) 大型文件的打印处理
假如有一个文本文件,包含精确到小数点后1 000 000位的圆周率值,只打印到小数点后50位,以免终端为显示全部1 000 000位而不断地翻滚:
filename = 'pi.txt'
with open(filename) as file_object:
lines = file_object.readlines()
pi_string = ''
for line in lines:
pi_string += line.strip()
print(pi_string[:52] + "...")
print(len(pi_string))
输出:
3.14159265358979323846264338327950288419716939937510...
1000002
(7) 写入空文件
filename = 'programming.txt'
with open(filename, 'w') as file_object:
file_object.write("I love python programming.-- PM Lab")
实参(‘w’)告诉Python,要以写入模式打开这个文件。打开文件时,可指定读取模式(‘r’)、 写入模式(‘w’)、 附加模式(‘a’)或能够读取和写入文件的模式(‘r+’)。如果省略了模式实参,Python将以默认的只读模式打开文件。
输出:
注意:Python只能将字符串写入文本文件。要将数值数据存储到文本文件中,必须先使用函数str()将其转换为字符串格式。
(8) 写入多行
filename = 'programming.txt'
with open(filename, 'w') as file_object:
file_object.write("I love programming.\n")
file_object.write("I love creating new games.\n")
输出:programming.txt
I love programming.
I love creating new games.
(9) 附加到文件
filename = 'programming.txt'
with open(filename, 'a') as file_object:
file_object.write("I also love finding meaning in large datasets.\n")
file_object.write("I love creating apps that can run in a browser.\n")
输出:programming.txt
I love programming.
I love creating new games.
I also love finding meaning in large datasets.
I love creating apps that can run in a browser.
可以看到,programming文本中附加了两行内容。