一、文件处理之控制文件内指针的移动
f.seek # 文件内指针移动,只有t模式下的read(n),n代表的字符的个数 # 除此以外文件内指针的移动都是以字节为单位 # with open('a.txt',mode='rt',encoding='utf-8') as f: # msg=f.read(1) # print(msg) # with open('a.txt',mode='rb') as f: # msg=f.read(3) # print(msg.decode('utf-8')) # f.seek(offset,whence)有两个参数: # offset: 代表控制指针移动的字节数 # whence: 代表参照什么位置进行移动 # whence = 0: 参照文件开头(默认的),特殊???,可以在t和b模式下使用 # whence = 1: 参照当前所在的位置,必须在b模式下用 # whence = 2: 参照文件末尾,必须在b模式下用 # with open('a.txt',mode='rt',encoding='utf-8') as f: # f.seek(6,0) # msg=f.read(1) # print(msg) # with open('a.txt',mode='rb') as f: # f.seek(3,0) # msg=f.read(3) # print(msg.decode('utf-8')) # with open('a.txt',mode='rb') as f: # msg=f.read(3) # # print(msg.decode('utf-8')) # print(f.tell()) # # f.seek(6,0) # f.seek(3,1) # msg1=f.read(3) # print(msg1.decode('utf-8')) # with open('a.txt',mode='rb') as f: # msg=f.read(3) # # print(msg.decode('utf-8')) # print(f.tell()) # # f.seek(6,0) # f.seek(3,1) # msg1=f.read(3) # print(msg1.decode('utf-8')) # with open('a.txt',mode='rb') as f: # # f.seek(0,2) # # print(f.tell()) # f.seek(-3,2) # msg=f.read(3) # print(msg.decode('utf-8')) # with open('access.log',mode='rb') as f: # f.seek(0,2) # 当前位置是147bytes # # while True: # line=f.readline() # 当前位置是196bytes # # print(f.tell()) # if len(line) == 0: # # 没有新的一行内容追加进来 # pass # else: # # 有新的一行内容追加进来 # print(line.decode('utf-8'),end='') # with open('access.log',mode='rb') as f: # f.seek(0,2) # 当前位置是147bytes # # while True: # line=f.readline() # 当前位置是196bytes # if len(line) != 0: # print(line.decode('utf-8'),end='') with open('a.txt',mode='r+t',encoding='utf-8') as f: f.truncate(6)
二、文件修改的两种方式
# 修改文件的方式一: # 1 将文件内容由硬盘全部读入内存 # 2 在内存中完成修改 # 3 将内存中修改后的结果覆盖写回硬盘 # 优点: 在文件修改的过程中硬盘上始终一份数据 # 缺点: 占用内存过多,不适用于大文件 # with open('d.txt',mode='rt',encoding='utf-8') as f: # all_data=f.read() # with open('d.txt',mode='wt',encoding='utf-8') as f: # f.write(all_data.replace('alex','dsb')) # 修改文件的方式二: # 1 以读的方式打开源文件,以写的方式打开一个临时文件 # 2 从源文件中每读一样内容修改完毕后写入临时文件,直到源文件读取完毕 # 3 删掉源文件,将临时文件重命名为源文件名 # 方式二: # 优点: 同一时刻在内存中只存在源文件的一行内容,不会过多地占用内存 # 缺点: 在文件修改的过程中会出现源文件与临时文件共存,硬盘上同一时刻会有两份数据,即在修改的过程中会过多的占用硬盘, # import os # with open('d.txt',mode='rt',encoding='utf-8') as read_f,open('.d.txt.swap',mode='wt',encoding='utf-8') as write_f: # for line in read_f: # write_f.write(line.replace('alex','dsb')) # os.remove('d.txt') # os.rename('.d.txt.swap','d.txt')
三、函数的基本使用
1 什么是函数 函数就是具备某一功能的工具 2 为什么要用函数 1 程序的组织结构不清晰,可读性差 2 代码冗余 3 可扩展性差 3 如何用函数 函数的使用必须遵循的原则:先定义,后调用 修理工事先准备工具的过程即函数的定义 修理工遇到应用场景拿来工具就用即函数的调用 语法: def 函数名(参数1,参数2,...): """ 文档注释 """ code1 code2 code3 ...... return 返回值 def:定义函数的关键字 函数名: 就相当于一个变量名,指向函数的内存地址, 注意:函数的内存地址()就可以出发函数体代码的执行 参数: 参数是函数的调用者为函数体代码传值的媒介,在python中函数的参数无需声明类型 """文档注释""" : 推荐写上 代码块:就是函数体功能的具体实现 return 返回值 :函数体代码块运行的成果 函数的使用分为两个阶段: 定义阶段: 只检测语法,不执行代码 调用阶段:运行函数体代码 def foo(): xxx print( ''' # 先定义 # def foo(): # print("from foo") # 后调用 # print(foo) # foo() # 定义时无参,意味着调用时也无需传入参数 # 先定义 # def bar(x,y): # print(x) # print(y) # 后调用 # bar('a',2) ## 定义时无参,意味着调用时也必须传入参数 # 定义无参函数:当函数体的代码逻辑不依赖任何传入的值就能执行,就不需要定义参数 # def print_msg(): # print('='*50) # print('welecome.....'.center(50,' ')) # print('='*50) # # print_msg() # print_msg() #定义有参函数:当函数体的代码逻辑依赖于外部调用者传入的值才能执行,必须定义参数用来接收外部传入的值 # def max2(x,y): # # x=1 # # y=3 # if x > y: # print(x) # else: # print(y) # max2(1,4) # def max2(x,y): # if x > y: # return x # else: # return y # res=max2(1,4) # print(res) # def foo(): # print('from foo') # bar() # foo() # def bar(): # print('from bar') # # def foo(): # print('from foo') # bar() # foo() # # 定义阶段 # def foo(): # print('from foo') # bar() # # def bar(): # print('from bar') # # # 调用阶段 # foo() # 定义阶段 def foo(): print('from foo') bar() # 调用阶段 foo() def bar(): print('from bar')