第三周文件处理和函数------上
===========第一节文件的读写 ---文件写 f=open(r'ppp.txt','wt',encoding='utf-8') print(f.writable()) f.write("dfjjhdffdfdfdfdqqqqqqqqqqq\n")#自己换行\n f.write("222222222222\n")#自己换行\n f.close() #一般在window上面需要添加r,取消window路径的问题 #rt 一般只写r代表省略了t,文本文件text #图片格式的就不需要编码. #编码和操作系统有关,windows上面是GBK # f=open(r'qq.txt','rt',encoding='utf-8') #写是覆盖 f=open(r'ppp.txt','wt',encoding='utf-8') print(f.writable()) f.write("dfjjhdffdfdfdfdqqqqqqqqqqq\n")#自己换行\n f.write("222222222222\n")#自己换行\n f.writelines(['t333t\n','kk4444kk\n']) #写多行 f.close() #a 模式文件不存在则创建文件,文件存在则打开文件,把光标移动到文件末尾 f=open(r'ppp.txt','at',encoding='utf-8') print(f.writable()) f.write("4444444444444444\n")#自己换行\n f.write("55555555555\n")#自己换行\n f.writelines(['77777\n','888888\n']) #写多行 f.close() -------------------一次读出所有文件 f=open(r'ppp.txt','rt',encoding='utf-8') print(f.read())# 一次读出所有文件,仅仅适用于文件很小的情况 f.close() -------------------readlines 一次读出放入列表 f=open(r'ppp.txt','rt',encoding='utf-8') # print(f.read())# 一次读出所有文件,仅仅适用于文件很小的情况 print(f.readlines())# 一次读出所有文件,仅仅适用于文件很小的情况 f.close() ['dfjjhdffdfdfdfdqqqqqqqqqqq\n', '222222222222\n', 't333t\n', 'kk4444kk\n', '4444444444444444\n', '55555555555\n', '77777\n', '888888\n'] ----------------------- f=open(r'ppp.txt','rt',encoding='utf-8') print(f.readline(),end='')#一行一行的读,多了一个换行符是print的end='',取消print的换行符 print(f.readline(),end='')#一行一行的读,多了一个换行符是print的end='',取消print的换行符 f.close() --------------------- 循环打印出文件内容: with open(r'ppp.txt','rt',encoding='utf-8') as f: while True: if len(f.readline())==0: break print(f.readline(),end='') ------------------打印出所有内容,for循环 with open(r'ppp.txt','rt',encoding='utf-8') as f: for line in f: print(line,end='') ----------------------------------------- -------------- with open(r'tt.txt','rb') as f: print(f.readline().decode('utf-8')) ========================= 二进制写txt with open(r'tt2.txt','wb') as f: print(f.write(('地方和环境').encode('utf-8'))) -------------二进制追加写 with open(r'tt2.txt','ab') as f: print(f.write( ('分担分担').encode('utf-8') ) ) ==================第二节 with open('tt2.txt','r',encoding='utf-8') as f: print(type(f)) 默认是读模式,读模式下不可以写. #一次读出所有文件的内容,第二次读取什么内容也无法读出,光标移动到了末尾, with open('tt2.txt','r',encoding='utf-8') as f: print(f.read()) print(f.read()) with open('tt2.txt','r',encoding='utf-8') as f: print(f.readline()) #读一行 #去除print的默认换行符 with open('tt2.txt','r',encoding='utf-8') as f: print(f.readline(),end='') print(f.readline(),end='') #读出所有行的内容,每行放入在一个列表 with open('tt2.txt','r',encoding='utf-8') as f: print(f.readlines()) ====文件写模式很危险会清空文件 >清空 with open('tt2.txt',mode='w',encoding='utf-8') as f: pass ===读出一个文件写入另一个文件,相当于CP功能. with open('tt2.txt',mode='r',encoding='utf-8') as r,\ open('tt3.txt',mode='w',encoding='utf-8') as w: for i in r: print(i) w.write(i) with open('tt2.txt',mode='r',encoding='utf-8') as r,\ open('tt3.txt',mode='w',encoding='utf-8') as w: for i in r: print(i) w.write(i) w.flush()#保证了写入的信息一定被刷新到了磁盘,因为系统有可能攒在内存里面隔一会写入 import sys print(sys.argv) #获取参数 #获取一个参数列表 文件名 和参数顺序 # ['D:\\PycharmProjects\\wadsd\\day3\\文件.py', 'aa', 'bb'] import sys print(sys.argv) #获取参数 _,srcfile,destfile=sys.argv #第一个参数不要第二三个参数解压. print(srcfile) print(destfile) ######cp 命令模仿最终版###################两个encoding 的作用是一个是python文件的编码,一个是处理文件复制文件的编码 #!/usr/bin/env python #encoding:utf-8 import sys print(sys.argv) #获取参数 _,srcfile,destfile=sys.argv print(srcfile) print(destfile) #获取一个参数列表 文件名 和参数顺序 # ['D:\\PycharmProjects\\wadsd\\day3\\文件.py', 'aa', 'bb'] with open(srcfile,mode='r',encoding='utf-8') as r,\ open(destfile,mode='w',encoding='utf-8') as w: for i in r: print(i) w.write(i) w.flush()#保证了写入的信息一定被刷新到了磁盘,因为系统有可能攒在内存里面隔一会写入 ===================================第三节文件的修改 --------模仿vim改文件 #!/usr/bin/env python #encoding:utf-8 import os with open('tt2.txt',mode='r',encoding='utf-8') as r,open('.swaptt2.txt',mode='w',encoding='utf-8') as w: for i in r: #data=i.replace('e','ax is sb') w.write(i.replace('e','ax is sb')) w.flush() os.remove('tt2.txt') os.rename('.swaptt2.txt','tt2.txt') ------------vim高效版本 import os with open('tt2.txt',mode='r',encoding='utf-8') as r,open('.swaptt2.txt',mode='w',encoding='utf-8') as w: for line in r: #data=i.replace('e','ax is sb') if 'g' in line: line=line.replace('g','ax is sb') w.write(line) w.flush() os.remove('tt2.txt') os.rename('.swaptt2.txt','tt2.txt') =======================第四节文件内光标的移动 ---只有这种情况是字符的情况读取,其他都是字节为单位 with open('tt2.txt','r',encoding='utf-8') as f: print(f.read(3)) with open('tt2.txt','rt',encoding='utf-8') as f: print(f.read(1)) f.seek(0,0) # 0 第一个表示开始的位置0代表文件的开头 第二个参数代表移动的位置 2,从第几个位置开始读,从那里后的几个位置 print(f.read()) #f.seek移动的是字节不是字符,一个汉字3个字节.只有0模式能在txt模式下使用,2是文件的末尾. #txt模式下是能用0. #只有一种情况光标以字符为单位:文件以rt方式打开,read(3) # with open('c.txt','rt',encoding='utf-8') as f: # # print(f.read(6)) # # print(f.tell()) # # f.seek(0,0) # # print(f.read(6)) # # # f.seek(6,0) # f.seek(8,0) # print(f.read()) ----到文件末尾,倒数三个字符. # with open('c.txt','rb') as f: # # f.seek(-3,2) # # print(f.tell()) # f.seek(0,2) -----------一个读取文件末尾的例子 import time with open(r'D:\PycharmProjects\wadsd\day3\tt3.txt','rb') as f: f.seek(0,2) while True: line=f.readline() if line: print(line) else: time.sleep(1) ===========第五节截断文件把文件从哪里到哪里保留下来 w模式的写会清空文件,a模式才能截断 import time with open(r'D:\PycharmProjects\wadsd\day3\tt3.txt','a',encoding='utf-8') as f: f.truncate(3) ----------从第三个字符清空文件 import time with open(r'D:\PycharmProjects\wadsd\day3\tt3.txt','a',encoding='utf-8') as f: f.truncate(3)
时来天地皆同力,运去英雄不自由