py08-文件处理-01
1、文件处理流程
1、打开文件,得到文件句柄并赋值给一个变量
2、通过句柄对文件进行操作
3、关闭文件
2、文件基本操作
f=open('a.txt','r',encoding='utf-8') print(f.read())
#一行一行的读,鼠标到最后一行
f=open('a.txt','r',encoding='utf-8') print(f.read()) print('第二次',f.read())
#第二次读取为空,因为文件已经全部读完,鼠标切到最后。第二次读取的时候已经为空
f=open('a.txt','r',encoding='utf-8') print(f.readline(),end='') print(f.readline(),end='') print(f.readline(),end='') print(f.readline(),end='')
#每次只读取一行内容
f=open('a.txt','r',encoding='utf-8')
print(f.readlines())
#读取所有内容,如果内容过大,会导致系统奔溃
f=open('a.txt','r',encoding='utf-8')
print(f.read())
f.close()
#打开文件,记着要关闭,不然会一直占用资源,这种方式需要手动关闭。另外一种用法,可以自动关闭
with open('a.txt','r',encoding='utf-8') as f: print(f.read())
#读取文件内容,然后自动关闭
3、文本模式的读r
文件不存在时候,不会创建新文件,会报错
with open('a.txt','r',encoding='utf-8') as f:
print(f.read())
4、文本模式的写w
文件存在则清空,文件不存在则创建
with open('a.txt','w',encoding='utf-8') as f:
f.write('111\n')
f.write('222\n')
f.write('您好\nhello\n杨许涛')
#一行一行的写文件
with open('a.txt','w',encoding='utf-8') as f: f.writelines(['杨许涛\n','hello\n','my name is xtyang'])
#写多行内容,并且换行
5、文本模式的追加a
文件存在,光标跳转到文件末尾,文件不存在则创建,鼠标会移动到末尾
with open('d.txt','a',encoding='utf-8') as f: f.write('123') f.writelines(['egon\n','xtyang\n','杨许涛'])
print(f.tell()) #打印光标的位置
#文件不存在会创建,然后写入文件内容
6、其他三种模式
r+:读的时候可以写
w+:写的时候可以读
a+:追加的时候可以读
with open('d.txt','r+',encoding='utf-8') as f: print(f.readable()) print(f.writable())
#查看文件是否可读,是否可写
7、文件的替换 replace,
with open('a.txt','r',encoding='utf-8') as read_f: msg=read_f.read() msg=msg.replace('old','sb') print(msg)
#把内容old替换成sb,这样的修改还是内存级别,没有保存到硬盘上。
import os with open('a.txt','r',encoding='utf-8') as read_f,\ open('.a.txt.swap','w',encoding='utf-8') as write_f: msg=read_f.read() msg=msg.replace('old','sb') write_f.write(msg) os.remove('a.txt') os.rename('.a.txt.swap','a.txt')
#替换,修改,刷新到硬盘,如果文件过大,会占资源。
import os with open('a.txt','r',encoding='utf-8') as read_f,\ open('.a.txt.swap','w',encoding='utf-8') as write_f: for line in read_f: if 'old' in line: line=line.replace('old','sb') write_f.write(line) os.remove('a.txt') os.rename('.a.txt.swap','a.txt')
#一行一行的替换,占用资源少。
8、rb模式的读即直接从硬盘中读取bytes
with open('a.txt','rb') as f: print(f.read())
(读取的bytes格式)
with open('a.txt','rb') as f:
print(f.read().decode('utf-8'))
(需要解码,才能看到人类能够懂的字符)
9、wb模式的写
with open('a.txt','wb') as f: f.write('你妹\n'.encode('utf-8'))
10、ab模式的追加
with open('a.txt','ab') as f: f.write('hello\n'.encode('utf-8')) f.write('中国'.encode('utf-8'))
11、图片格式的文件操作
图片的拷贝
with open('test.png','rb') as read_f,\ open('test1.png','wb') as write_f: for line in read_f: write_f.write(line)
12、传参模块sys
import sys print(sys.argv) sys.exit()
执行结果:
E:\py18\练习>python copy.py a b c d ['copy.py', 'a', 'b', 'c', 'd']
(是一个列表的形式,脚本是下标0,参数下标依次)
13、拷贝文件传参例子:
import sys #python3 copy.py source.file target.file #判断传入的参数 if len(sys.argv) < 3: print('Usage:python3 copy.py source.file target.file ') sys.exit() with open(sys.argv[1],'rb') as read_f,\ open(sys.argv[2],'wb') as write_f: for line in read_f: write_f.write(line)
执行:
E:\py18\练习>python3 copy.py test.png c:\new.png
www.sysgit.com

浙公网安备 33010602011771号