文件的使用方式

mac2022-06-30  61

1.补充的文件处理模式"""rwa上面的三个模式称为纯净模式r+w+a+"""# with open(r'test',mode='r+',encoding='utf-8') as f:# print(f.readable()) # print(f.writable())# print(f.readline())# f.write('嘿嘿嘿')

# with open(r'test',mode='w+',encoding='utf-8') as f:# print(f.readable())# print(f.writable())# print(f.readline())# f.write('嘿嘿嘿')

# with open(r'test',mode='r+b') as f:# print(f.readable())# print(f.writable())# res = f.read()# # print(res.decode('utf-8'))# res1 = str(res,encoding='utf-8')# print(res1) # 补充的的模式都属于可读可写类

2.# 在rt模式下 read内的数字 表示的是字符的个数# 初次之外,数字表示的都是字节# with open(r'test','r',encoding='utf-8') as f:# print(f.read(5))

# with open(r'test','rb') as f:# res = f.read(10) # 读的是三个字节bytes# print(res)# print(res.decode('utf-8'))

# 文件内光标的移动"""f.seek(offset,whence)offset:相对偏移量 光标移动的位数whence: 0:参照文件的开头 t和b都可以使用 1:参照光标所在的当前位置 只能在b模式下用 2:参照文件的末尾 只能在b模式下使用"""# with open(r'test','rt',encoding='utf-8') as f:# print(f.read(1))# # f.seek(6,0) # seek移动都是字节数 从0开始的第6位# print(f.read(1))# 读取一个# f.seek(0, 0)# print(f.read())# 读取全部

# with open(r'test','rb') as f:# print(f.read(1).decode('utf-8')) # rb模式读出的万国码需要转格式# # f.seek(6,0)

# with open(r'test','rb') as f:# print(f.read(3).decode('utf-8'))# f.seek(3,1)# print(f.read(1))# f.seek(6,0) # seek移动都是字节数

# with open(r'test','rb') as f:# print(f.read())# f.seek(-4,2)# print(f.read().decode('utf-8'))

# with open(r'test','r+',encoding='utf-8') as f:# f.seek(3,0)# f.write('过')

3.写日志import time

res = time.strftime('%Y-%m-%d %X') #导入一个库# print(res,type(res))

with open(r'test01.txt', 'a', encoding='utf-8') as f: f.write('%s egon给jason发了1个亿的工资\n' % res) # 可在末尾循环追加当前时间

4.检测所写的日志with open(r'test01.txt', 'rb') as f: # 先将光标移动到文件末尾 f.seek(0, 2) while True: res = f.readline() # 查看光标移动了多少位 bytes # print(f.tell()) if res: print("新增的文件内容:%s" % res.decode('utf-8')) # 说明有人操作当前文件 # else: # # 说明文件没有被任何人操作 # print('暂无其他人操作该文件') # 此while 循环不会终端5.with open(r'test', 'a', encoding='utf-8') as f: f.truncate(6) # 接收的字节的长度 整型 # 保留0~6字节数 后面的全部删除(截断)

6.# 修改文件# 先将数据由硬盘读到内存(读文件)# 在内存中完成修改(字符串的替换)# 再覆盖原来的内容(写文件)# with open(r'test02.txt','r',encoding='utf-8') as f:# data = f.read()# print(data)# print(type(data))## with open(r'test02.txt','w',encoding='utf-8') as f:# res = data.replace('egon','jason')# print(data)# f.write(res)

"""优点:任意时间硬盘上只有一个文件 不会占用过多硬盘空间缺点:当文件过大的情况下,可能会造成内存溢出"""

# 文件修改方式2# 创建一个新文件# 循环读取老文件内容到内存进行修改 将修改好的内容写到新文件中# 将老文件删除 将新文件的名字改成老文件名import os

with open(r'test02.txt','r',encoding='utf-8') as read_f,\ open(r'test02.swap','a',encoding='utf-8') as write_f: for line in read_f: new_line = line.replace('jason','egon') write_f.write(new_line)os.remove('test02.txt')os.rename('test02.swap','test02.txt')

"""优点:内存中始终只有一行内容 不占内存缺点:再某一时刻硬盘上会同时存在两个文件

6.函数与变量的区别s = 'hello'# print(len(s))# 假设没有len了# 函数体代码定义截断只检测语法 不执行代码def test(): hdhfd sdafsd sdafjsldakj sdfljsda;lf;lskdf;l

print(test())"""可以通过变量名找到变量对应的值可以通过函数名+括号 找到函数体所对应的代码并执行"""

# 函数名的命名规则跟变量名一模一样

# 函数就是工具,并且函数必须先定义后调用(函数名+括号)

转载于:https://www.cnblogs.com/night-rain/p/11154839.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)