莫烦python基础教程(六)

文件读取

读写文件1


\n 换行命令

  • 定义text为字符串,并查看使用\n和不使用\n的区别
text = 'This is my first test.This is the second line.This is the third line.'
print(text)     # 无换行命令

'''
输出结果:This is my first test.This is the second line.This is the third line.
'''

text = 'This is my first test.\nThis is the second line.\nThis is the third line.'
print(text)     # 输入换行命令\n,要注意斜杠的方向,注意换行的格式与C++一样

'''
输出结果:
This is my first test.
This is the second line.
This is the third line.
'''

open 读文件方式

  • 使用 open 能够打开一个文件,open 的第一个参数为文件名和路径 ‘my file.txt’,第二个参数为将要以什么方式打开它, 比如 w 为可写方式。如果计算机没有找到 ‘my file.txt’ 这个文件, w 方式能够创建一个新的文件,并命名为 my file.txt。
text = 'This is my first test.\nThis is the second line.\nThis is the third line.'

my_file = open('my_file.txt', 'w')
# 用法:open('文件名', '打开方式'),其中打开方式为'w','r'
my_file.write(text)     # 该语句会写入先前定义好的text
my_file.close()     # 关闭文件

\t tab 对齐

  • 使用 \t 能够达到 tab 对齐的效果。
text = '\tThis is my first test.\n\tThis is the second line.\n\tThis is the third line.'
print(text)     # 延伸使用\t对齐

'''
    This is my first test.
    This is the second line.
    This is the third line.
'''

读写文件2


给文件增加内容

  • 我们先保存一个已经有3行文字的 “my file.txt” 文件, 文件的内容如下:
This is my first test.
This is the second line.
This is the third line.
  • 然后使用添加文字的方式给这个文件添加一行 “This is appended file.”,并将这行文字储存在 append_file 里:
text = 'This is my first test.\nThis is the second line.\nThis is the third line.'

append_text = '\nThis is appended file.'     # 为这行文字提前空行“\n”
my_file = open('my_file.txt', 'a')       # 以增加内容的方式打开
my_file.write(append_text)
my_file.close()

'''
This is my first test.
This is the second line.
This is the third line.
This is appended file.
'''
# 运行后再去打开文件,发现会增加一行代码中定义的字符串
  • append 的用法 :open(‘my file.txt’,’a’) 打开类型为 a ,a 即表示 append。

读写文件3


读取文件内容 file.read()

  • 使用 file.read() 能够读取到文本的所有内容。
text = 'This is my first test.\nThis is the second line.\nThis is the third line.'

file = open('my_file.txt', 'r')
content = file.read()
print(content)

'''
This is my first test.
This is the second line.
This is the third line.
This is appended file.
'''

按行读取 file.readline()

  • 如果想在文本中一行行的读取文本,可以使用 file.readline(),file.readline() 读取的内容和你使用的次数有关,使用第二次的时候,读取到的是文本的第二行,并以此类推。
text = 'This is my first test.\nThis is the second line.\nThis is the third line.'

my_file = open('my_file.txt', 'r')
content = my_file.readline()   
print(content)

'''
This is my first test.
'''

second_read_time = my_file.readline() 
print(second_read_time)

'''
This is the second line.
'''

读取所有行 file.readlines()

  • 如果想要读取所有行,并可以使用像for一样的迭代器迭代这些行, 我们可以使用 file.readlines(),将每一行的结果存储在list中,方便以后迭代。
text = 'This is my first test.\nThis is the second line.\nThis is the third line.'

my_file = open('my_file.txt', 'r')
content = my_file.readlines()  
print(content)

'''
['This is my first test.\n', 'This is the second line.\n',
'This is the third line\n', 'This is appended file.']
'''

for item in content:
    print(item)

'''
This is my first test.

This is the second line.

This is the third line.

This is appended file.
'''

猜你喜欢

转载自blog.csdn.net/faker1895/article/details/81812926
今日推荐