python文件操作
1、从文件中读取数据
1.1、读取整个文件
新建一个txt文本文件:
is_digits.txt 3.1415926535 8979323846 2643383279
新建一个.py文件
1 # -*-coding:utf-8 -*- 2 3 with open("pi_digits.txt") as file_object: 4 contents = file_object.read() 5 print(contents)
执行结果:
3.1415926535 8979323846 2643383279
1.2、文件路径:
通过文件所在路径,进行文本读取操作
1 #文件路径 2 3 file_path = 'C:\pycharm\python3\pi_digits.txt' 4 5 with open(file_path) as file_object: 6 contents = file_object.read() 7 print(contents.rstrip()) #rstrip()是删除文本末尾的空白
执行结果:
3.1415926535 8979323846 2643383279
1.3、逐行读取
对文本的每一行进行读取操作:
1 #逐行读取 2 3 with open('pi_digits.txt') as file_object: 4 for line in file_object: 5 print(line)
执行结果:
3.1415926535
8979323846
2643383279
如何消除多余的空白呢,使用rstrip()函数
with open('pi_digits.txt') as file_object: for line in file_object: # print(line) print(line.rstrip())
执行结果:
3.1415926535 8979323846 2643383279
1.3、创建一个包含文件各行内容的列表
知识点:
- readlines()
- for ... in ... 循环
- str.rstrip()函数
实例:
1 #创建一个包含文件各行的内容 2 3 filename = "pi_digits.txt" 4 5 with open(filename) as file_objects: 6 lines = file_objects.readlines() 7 8 for line in lines: 9 print(line。rstrip())
执行结果:
3.1415926535 8979323846 2643383279
1.4、使用文件的内容
#使用文件的内容
filename = "pi_digits.txt"
with open(filename) as file_objects:
lines = file_objects.readlines()
pi_string = '' #定义一个空字符串
for line in lines:
pi_string += line.rstrip() #将每一行的数字进行加法(也就是放到一行中),并且删除多余空格
print(pi_string)
print(len(pi_string)) #打印字符串长度
执行结果:
3.141592653589793238462643383279 32
2、写入文件
保存数据的最简单方式就是把它写到文件中。
2.1、写入空文件
方法:
- 'w' :表示写入模式
- ‘r’:表示读取模式
- ‘a’:表示附加模式
- write():文件对象写入方法
实例:
1 #写入空文件 2 filename = "new_file.txt" #定义一个文件名称 3 4 with open(filename,'w') as file_object: # 'w'表示读取模式 5 file_object.write('I love python.')
执行结果:
2.2、写入多行
注意审题,是写入多行,而不是一行哦。
代码:
1 #写入多行 2 3 filename = 'new_file2.txt' 4 with open(filename,'w') as file_object: 5 file_object.write('Hello,world.') 6 file_object.write("As the old saying goes, where there's a will, there's a way")
输出结果:
Hello,world.As the old saying goes, where there's a will, there's a way
怎么样才能进行换行操作实现多行呢, 使用 “\n”
代码:
1 filename = 'new_file2.txt' 2 with open(filename,'w') as file_object: 3 file_object.write('Hello,world.\n') 4 file_object.write("As the old saying goes, where there's a will, there's a way\n")
输出:
Hello,world. As the old saying goes, where there's a will, there's a way
2.3、附加到文件
如果想在原有的文件基础上继续增加内容,而不是每次都新建一个新文件,怎么操作呢,这里使用附件操作。
知识点:
- ‘a’:附件模式
- ‘r+’:读取和写入模式
实例:
1 #附件模式 2 filename = 'new_file2.txt' 3 with open(filename,'a') as file_object: 4 file_object.write('Thinking is impossible.\n') 5 file_object.write('That is ok\n')
执行结果:
1 Hello,world. 2 As the old saying goes, where there's a will, there's a way 3 Thinking is impossible. 4 That is ok 5 Thinking is impossible. 6 That is ok
方法2:
1 #方法2:r+模式 2 filename = 'new_file2.txt' 3 with open(filename,'r+') as file_object: 4 file_object.write(' process finished with exit code 0.\n')
结果:
process finished with exit code 0. 0. e there's a will, there's a way Thinking is impossible. That is ok Thinking is impossible. That is ok