文件操作
文件处理步骤:
f ,变量f_obj,f_handler,f_h,fh------文件句柄。open(),python的内置函数,(内部调用的是windows的系统命令)
1.打开文件,产生文件句柄。
2.对文件句柄进行操作
3.关闭文件句柄
读:
r 对于r模式,mode可以默认不写。
全读:
f = open('d:\文件操作.txt',encoding='utf-8',mode ='r')
content = f.read()
print(content)
f.close()
read(n),按字符读取
f = open('d:\文件操作.txt',encoding='utf-8',mode='r')
content = f.read(1)
print(content)
f.close()
readline.按行读取
f = open('d:\文件操作.txt',encoding='utf-8',mode='r')
content = f.readline()
print(content)
f.close()
readlines,按行读取,返回list
f = open('d:\文件操作.txt',encoding='utf-8',mode='r')
content = f.readlines()
content = ''.join(content).strip()
print(content)
f.close()
for循环。
f = open(r'd:\文件操作.txt',encoding='utf-8',mode='r')
for line in f:
print(line.strip())
f.close()
2.rb,文件操作中凡是带‘b’的都是非文字的操作,如图片,视屏
f = open('d:\图片.jpg',mode='rb')
content = f.read()
print(content)
f.close(
按字节提取:
f = open('d:\图片.jpg',mode='rb')
content = f.read(3)
print(content)
f.close()
r+,追加,先读后追加:
f = open('d:\文件操作.txt',encoding='utf-8',mode='r+')
content = f.read()
print(content)
f.write('12345') #写入的必须是字符串
f.close()
r+,不读直接写,会覆盖掉原来文件的所有内容。
f = open('d:\文件操作.txt',encoding='utf-8',mode='r+')
f.write('深圳你好')
f.close()
w.写。
没有文件,创建文件也要写。有文件,先清空文件,再写入。
f = open('d:\文件操作.txt',encoding='utf-8',mode='w')
content = f.write('中华人民共和国,万岁')
print(content)
f.close()
wb模式
f = open('d:\图片.jpg',mode='rb')
content = f.read()
f.close()
print(content)
f1 = open('d:\图片1.jpg',mode='wb')
f1.write(content)
f1.close()
w+
f = open('d:\文件操作.txt',encoding='utf-8',mode='w+')
f.write('123456123456')
f.seek(3)
content = f.read()
print(content)
f.close()
a 追加:
没有文件,创建文件也要追加,有文件则在文件最后追加。
f = open(r'd:\文件操作.txt',encoding='utf-8',mode='a')
f.write('人生百年')
f.close()
其他方法:
readable,writable,seek
f = open('d:\文件操作.txt',encoding='utf-8')
if f.readable():
content = f.read()
print(content)
f.close()
f = open('d:\文件操作.txt',encoding='utf-8',mode='a')
if f.writable():
content =f.write('天下第一')
print(content)
f.close()
seek,,调整光标开始位置,开始seek(0),seek(6)6给字节开始,seek(0,2)光标调整到末尾
f = open(r'd:\文件操作.txt',encoding='utf-8',mode='r') print(f.read()) f.seek(0,2)# 按照字节去移动光标 content = f.read() print(content) f.close()
truncate要在writable模式下才能进行截取
并且要在r+,a+下进行,不能在w下进行
f = open(r'd:\文件操作.txt',encoding='utf-8',mode='a+') print(f.truncate(3)) f.close()
主动关闭句柄:
with open(r'D:\文件操作.txt',encoding='utf-8') as f1:
print(f1.read())
开启多个句柄:
with open('d:\文件操作.txt',encoding='utf-8') as f1,\
open('d:\文件操作1.txt',encoding='utf-8',mode='r') as f2:
print(f1.read())
f2.read()
文件改的操作:
1.已读的模式打开原文件,产生一个文件句柄f1.
2.以写的模式创建一个新文件,产生一个文件的句柄f2.
3.读取原文件的内容,进行修改,并将修改后的写入新文件。
4.将原文件删除
5.将新文件重命名成原文件。
low版:
import os
with open('d:\文件操作.txt',encoding='utf-8') as f1,\
open('d:\文件操作3.txt',encoding='utf-8',mode='w') as f2:
old_content = f1.read()
new_content = old_content.replace(1,3)
f2.write(new_content)
os.remove('文件操作.txt')
os.rename('文件操作3.txt')
最终版:
import os
with open('d:\文件操作.txt',encoding='utf-8') as f1,\
open('d:\文件操作3.txt',encoding='utf-8',mode='w') as f2:
for line in f1:
new_line = line.replace(1,2)
f2.write(nem_line)
os.remove('文件操作.txt')
os.rename('文件操作3.txt')

浙公网安备 33010602011771号