第三章 读写文件

1.按照文件路径类型分类                                                      

# 1.绝对路径读取文件的操作
f1 = open('D:\read_and_write.txt', encoding='utf-8', mode='r')
content = f1.read()
print(content)
f1.close()
# apple
# orange
# banana

# 2.绝对路径读取非文字模式文件的操作,不用添加encoding
f2 = open('D:\read_and_write.txt', mode='rb')
print(f2.read())
f2.close()
# b'apple
orange
banana'

# 3.相对路径下读取文件的操作,mode默认值为r,可以不添加
f1 = open('register', encoding='utf-8')
content = f1.read()
print(content)
f1.close()
# Bge 男
# 美鑫 男
# 天儿 男

# 4.相对路径读取非文字模式文件的操作,不用添加encoding
f1 = open('register', mode='rb')
content = f1.read()
print(content)
f1.close()
# b'Bge xe7x94xb7
xe7xbex8exe9x91xab xe7x94xb7
xe5xa4xa9xe5x84xbf xe7x94xb7'

2.文件读取方式的分类                                                     

# 按文件读取方式分类
# 1.read()全部读取
f1 = open('register', encoding='utf-8', mode='r')
content = f1.read()
print(content)
f1.close()
# Bge 男
# 美鑫 男
# 天儿 男

# 2.read(3),r模式按照字符读取文件
'''
文件名:水果
苹果香蕉橘子
'''
f1 = open('水果', encoding='utf-8', mode='r')
content = f1.read(3)
print(content)
f1.close()
# 苹果香

# read(3),rb模式按照字节读取文件,注意中文占三个字节
f1 = open('水果', mode='rb')
content = f1.read(3)
print(content)
f1.close()
# b'xe8x8bxb9'

# 3.readline(),读取文件的一行
'''
文件名:水果
苹果
香蕉
橘子
'''
f1 = open('水果', encoding='utf-8')
print(f1.readline())
# 苹果
print(f1.readline())
# 香蕉
print(f1.readline())
# 橘子
f1.close()

# 4.readlines(),读取文件的所有行,然后把它们作为一个字符串列表返回
'''
文件名:水果
苹果
香蕉
橘子
'''
f1 = open('水果', encoding='utf-8')
l1 = f1.readlines()
print(l1)
f1.close()
# ['苹果
', '香蕉
', '橘子']

# 5.for循环,f1代表迭代器,在内存中只占一行内存-->推荐方式(但依然需要在结尾close())
'''
文件名:水果
苹果
香蕉
橘子
'''
f1 = open('水果', encoding='utf-8')
for i in f1:
    print(i)
f1.close()
# 苹果
#
# 香蕉
#
# 橘子
#

# 6.因为python有回收机制,所以打开文件的时候有一种with open() as 的方法,可以不用写最后close()
# 单个文件
with open('水果', encoding='utf-8') as f1:
# 多个文件
with open('水果', encoding='utf-8') as f1,
    open('register', encoding='utf-8') as f2:

3.文件的读写类型分类                                                    

读:r rb r+ rb+

# r/rb 读取文件/读取非文字模式的文件
# r
f1 = open('水果', encoding='utf-8', mode='r')
print(f1.read())
f1.close()
# 苹果
# 香蕉
# 橘子

# rb
f1 = open('水果', mode='rb')
print(f1.read())
f1.close()
# b'xe8x8bxb9xe6x9ex9c
xe9xa6x99xe8x95x89
xe6xa9x98xe5xadx90'

# r+/rb+ 先读取原文件(非文字模式的文件),再写文件
# r+
f2 = open('水果', encoding='utf-8', mode='r+')
print(f2.read())
# 苹果
# 香蕉
# 橘子
f2.write('666')
f2.close()
# 这时文件内容已经变为下面这样
# 苹果
# 香蕉
# 橘子666

# rb+
f2 = open('水果', mode='rb+')
print(f2.read())
# b'xe8x8bxb9xe6x9ex9c
xe9xa6x99xe8x95x89
xe6xa9x98xe5xadx90'
f2.write('666'.encode())
f2.close()
# 这时文件内容已经变为下面这样
# 苹果
# 香蕉
# 橘子666

写:w wb w+ wb+

# w/wb 写文件,如果文件不存在,创建并写入文件,如果文件存在则覆盖
# w
f1 = open('newfile', encoding='utf-8', mode='w')
f1.write('6666') # 写入文件的类型必须是字符串
f1.close()

# wb
f1 = open('newfile', mode='wb')
f1.write('6666'.encode())
f1.close()
# 当写入给文字模式的文件时,不进行编码,写入文件会报错
f1 = open('newfile', mode='wb')
f1.write('6666')
f1.close()
#     f1.write('6666')
# TypeError: a bytes-like object is required, not 'str'

# w+/wb+ 先写后读,写读模式,用的不多
# w+ 先写后读,下面例子文件会更改,但程序不会输出结果,因为写入文件后,光标移到文件的最后,打印不出东西
f1 = open('newfile', encoding='utf-8', mode='w+')
f1.write('6666')
print(f1.read())
f1.close()
# 正确方式
f1 = open('newfile', encoding='utf-8', mode='w+')
f1.write('6666')
f1.seek(0)
print(f1.read())
f1.close()
# 6666

# wb+
f1 = open('newfile', mode='wb+')
f1.write('6666'.encode())
f1.seek(0)
print(f1.read())
f1.close()
# b'6666'

追加:a ab a+ ab+

# 追加:a ab a+ ab+
# a/ab 追加,原内容不变,在结尾追加写入的内容
# a
f1 = open('水果', encoding='utf-8', mode='a')
f1.write('
葡萄')
f1.close()
# ab
f1 = open('水果', mode='ab')
f1.write('
葡萄'.encode())
f1.close()

# a+/ab+ 先追加,然后再读,不调光标同样不会输出结果
f1 = open('水果', encoding='utf-8', mode='a+')
f1.write('
葡萄')
print(f1.read())
f1.close()

# 正确方式
# seek(参数),seek(0,2)调至最后,按照字节去调整
f1 = open('水果', encoding='utf-8', mode='a+')
f1.write('
葡萄')
f1.seek(0)
print(f1.read())
f1.close()
# 苹果
# 香蕉
# 橘子
# 葡萄

4.修改文件的详细说明                                                           

# 所有操作文件软件,修改文件的方法
# 1- 打开原文件,产生文件句柄。
# 2- 创建新文件,产生文件句柄。
# 3- 读取原文件,进行修改,写入新文件。
# 4- 将源文件删除。
# 5- 新文件重命名原文件。

# 方法一:直接修改
import os
with open('水果', encoding='utf-8', mode='r') as f1,
    open('水果.bak', encoding='utf-8', mode='w') as f2:
    old_content = f1.read()
    new_content = old_content.replace('苹果', 'apple')
    f2.write(new_content)
os.remove('水果')
os.rename('水果.bak', '水果')

# 方法二:使用for循环修改
import os
with open('水果', encoding='utf-8', mode='r') as f1,
    open('水果.bak', encoding='utf-8', mode='w') as f2:
    # 只要进入for循环,没有停止,只是进行了一次操作,相当于for循环结束后一次性写入
    for line in f1:
        new_line = line.replace('apple', '苹果')
        f2.write(new_line)
os.remove('水果')
os.rename('水果.bak', '水果')

5.工作中遇到问题                                                                

'''
file1
192.168.1.1
192.168.1.2
file2
192.168.1.1:192.168.5.1
192.168.1.2:192.168.5.2
192.168.1.3:192.168.5.3
要求:判断file1中的IP是否在file2中
'''
# 错误代码: with open('file1', encoding='utf-8') as f1, open('file2', encoding='utf-8') as f2: for i in f1: i = i.strip() for j in f2: j = j.strip() if i in j: print(j) # 结果只打印了一行,但是后面的结果没有输出 # 192.168.1.1:192.168.5.1 # 正确代码 with open('file1', encoding='utf-8') as f1, open('file2', encoding='utf-8') as f2: for i in f1: i = i.strip() for j in f2: j = j.strip() if i in j: print(j) f2.seek(0) # ********************* # 原因是内层for循环会从头到尾搜索f2,最终光标移动到文件末尾,所以之后就不会出现结果了 # 应该添加seek(0),每次内层循环结束后,将f2的光标移动到文件的开头 # 192.168.1.1:192.168.5.1 # 192.168.1.2:192.168.5.2
一鼓作气,再而衰,三而竭。
原文地址:https://www.cnblogs.com/gongniue/p/8874385.html