标签:内存溢出 日志 win 找不到 exist 移动 硬盘 NPU 开头
用户/应用程序
操作系统(文件)
计算机硬件(硬盘)
1.读写都是以str(Unicode)为单位的
2.文本文件
3.必须为open()指定encoding=‘utf-8‘
控制文件读写操作的模式:
r:只读模式
w:只写模式
a:只追加写模式
+:r+、w+、a+
open(‘C:\a\nb\c\d.txt‘)
open(r‘C:\a\nb\c\d.txt‘) # 路径前面加r,取消转义字符的作用
open(‘C:/a/nb/c/d.txt‘) # 系统自动识别为路径
f = open(r‘F:\Python学习相关\world.txt‘,mode=‘rt‘) # f的值是一种变量,占用的是应用程序的内存空间 print(f) x = 10 # 属于 应用程序:Python解释器 的内存空间
绝对路径:(就是文件的完整路径)
优点:路径完整,易于寻找
缺点:路径名可能过长,前路径修改后,文件就找不到
相对路径:(当前文件夹所在的路径)
优点:路径相对剪短
缺点:文件换个文件夹就不以寻找
f=open(r‘C:\Users\Darker\Desktop\aaa.txt‘,mode=‘rt‘,encoding=‘UTF-8‘) res = f.read() print(res)
with open(r‘C:\Users\Darker\Desktop\aaa.txt‘,mode=‘rt‘,encoding=‘UTF-8‘) as f: res = f.read() print(res)
del f f.close()
with open(r‘C:\Users\Darker\Desktop\aaa.txt‘,mode=‘rt‘,encoding=‘UTF-8‘) as f1, open(r‘C:\Users\Darker\Desktop\aaa.txt‘,mode=‘rt‘,encoding=‘UTF-8‘) as f2: res1 = f1.read() res2 = f2.read() print(res1) print(res2)
with open(r‘C:\Users\Darker\Desktop\aaa.txt‘,mode=‘rt‘) as f1: res1 = f1.read() # t模式会将f.read()读出的结果解码成Unicode print(res1,type(res1)) # 此时,就会报错: UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x80 in position 16: illegal multibyte sequence
内存:utf-8格式的二进制----解码----Unicode
硬盘(aaa.txt内容:utf-8的二进制)
with open(r‘C:\Users\Darker\Desktop\aaa.txt‘,mode=‘rt‘,encoding=‘UTF-8‘) as f: print(‘第一次读‘.center(50,‘*‘)) res = f.read() # 把所有内容从硬盘读到内存 print(res) print(‘第二次读‘.center(50, ‘*‘)) res1 = f.read() # t模式会将f.read()读出的结果解码成Unicode print(res1) ***********************第一次读*********************** 滚滚长江东逝水 滚滚长江东逝 滚滚长江东 滚滚长江 滚滚长 滚滚 滚 ***********************第二次读***********************
user.txt中的内容: qwe:123 asd:456 zxc:789 inp_username = input(‘Your name:‘).strip() inp_password = input(‘Your password:‘).strip() with open(‘user.txt‘,mode=‘rt‘,encoding=‘UTF-8‘) as f: for line in f: l = line.strip().split(‘:‘) username,password = line.strip().split(‘:‘) if inp_username == username and inp_password == password: print(‘Successful‘) break else: print(‘False‘)
with open(‘d.txt‘,mode=‘wt‘,encoding=‘utf-8‘) as f: # f.read() #只能写,不能读 f.write(‘111\n123‘) # 先清空内容,再写入
在以w模式打开文件没有关闭的情况下,连续写入,新写的内容总是跟在旧写的内容之后
with open(‘d.txt‘,mode=‘wt‘,encoding=‘utf-8‘) as f: # f.read() #只能写,不能读 f.write(‘111\n‘) # 先清空内容,再写入 f.write(‘222\n‘) # 先清空内容,再写入 f.write(‘333\n‘) # 先清空内容,再写入
如果重新以w模式打开文件,则会清空文件
with open(‘d.txt‘,mode=‘wt‘,encoding=‘utf-8‘) as f: f.write(‘111\n‘) # 先清空内容,再写入 with open(‘d.txt‘,mode=‘wt‘,encoding=‘utf-8‘) as f: f.write(‘222\n‘) # 先清空内容,再写入 with open(‘d.txt‘,mode=‘wt‘,encoding=‘utf-8‘) as f: f.write(‘333\n‘) # 先清空内容,再写入
针对文本文件的拷贝Copy工具
src_file = input(‘原文件路径:‘).strip() dst_file = input(‘原文件路径:‘).strip() with open(r‘{}‘.format(src_file),mode=‘rt‘,encoding=‘utf-8‘) as f1, open(r‘{}‘.format(dst_file),mode=‘wt‘,encoding=‘utf-8‘) as f2: res = f1.read() f2.write(res)
with open(‘e.txt‘,mode=‘at‘,encoding=‘utf-8‘) as f: # f.read() # 报错,不能读 f.write(‘我去1\n‘) f.write(‘我去2\n‘) f.write(‘我去3\n‘)
在打开文件 不关闭的情况下,连续的写入,新写的内容总会跟在之前写的内容之后
以a模式重新打开文件,不会清空原文件内容,会将文件指针直接移动到文件末尾
以为w模式重新打开文件,会清空原文件内容,会将文件指针移动到文件开头
注册功能: name = input(‘请输入用户名:‘) pwd = input(‘请输入密码:‘) with open(‘db.txt‘,mode=‘at‘,encoding=‘utf-8‘) as f: f.write(‘{}:{}\n‘.format(name,pwd))
a.txt存在,就会报错 with open(r‘a.txt‘,mode=‘x‘,encoding=‘UTF-8‘) as f: f.read() FileExistsError: [Errno 17] File exists: ‘a.txt‘
b.txt不存在,就会创建 with open(r‘b.txt‘,mode=‘x‘,encoding=‘UTF-8‘) as f: f.write(‘haha\n‘)
with open(r‘预见未来.rmvb‘,mode=‘rt‘) as f: f.read() # 硬盘的二进制读入内存 --> t模式会将读入内存的内容进行decode解码
with open(r‘预见未来.rmvb‘,mode=‘rb‘) as f: res = f.read() # 硬盘的二进制读入内存 --> b模式下,不做任何转换,直接读入内存 print(res) # bytes类型--》当成二进制 # OverflowError: bytes object is too large to make repr # 溢出错误:bytes对象太大,无法生成repr with open(r‘b.txt‘,mode=‘rb‘) as f: res = f.read() # 硬盘的二进制读入内存 --> b模式下,不做任何转换,直接读入内存 print(res,type(res)) print(res.decode(‘utf-8‘)) b.txt内容是:“哈哈哈” 输出b‘\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88‘ 16进制显示 说明,一个中文字符,对应3个bytes with open(r‘b.txt‘,mode=‘rt‘,encoding=‘utf-8‘) as f: res = f.read() # UTF-8的二进制--》unicode print(res) # print(res.decode(‘utf-8‘))
with open(r‘b.txt‘,mode=‘wt‘,encoding=‘utf-8‘) as f: f.write(‘你好hello‘.encode(‘utf-8‘)) f.write(‘哈哈哈‘.encode(‘gbk‘))
src_file = input(‘原文件路径:‘).strip() dst_file = input(‘新文件路径:‘).strip() with open(r‘{}‘.format(src_file),mode=‘rb‘,encoding=‘utf-8‘) as f1, open(r‘{}‘.format(dst_file),mode=‘wb‘,encoding=‘utf-8‘) as f2: # res = f1.read() # 内存占用过大 # f2.write(res) for line in f1: f2.write(line)
图片、视频每行都很简短,多的用while,少的用for
方式1 with open(r‘关机64.ico‘,mode=‘rb‘) as f: while True: res = f.read(1024) # 一行读取1024个字节 if not res: break print(len(res)) 1024 1024 1024 1024 1024 1024 1024 1024 1024 1024 1024 1024 1024 1024 1024 1024 574 方式2 with open(r‘a.txt‘,mode=‘rt‘,encoding=‘utf-8‘) as f: for line in f: print(line) 滚滚长江东逝 滚滚长江东 滚滚长江 滚滚长 滚滚 滚
Windows模式下,换行符是 "\r\n"
with open(r‘a.txt‘,mode=‘rb‘) as f: for line in f: print(line) b‘\xe6\xbb\x9a\xe6\xbb\x9a\xe9\x95\xbf\xe6\xb1\x9f\xe4\xb8\x9c\xe9\x80\x9d\r\n‘ b‘\xe6\xbb\x9a\xe6\xbb\x9a\xe9\x95\xbf\xe6\xb1\x9f\xe4\xb8\x9c\r\n‘ b‘\xe6\xbb\x9a\xe6\xbb\x9a\xe9\x95\xbf\xe6\xb1\x9f\r\n‘ b‘\xe6\xbb\x9a\xe6\xbb\x9a\xe9\x95\xbf\r\n‘ b‘\xe6\xbb\x9a\xe6\xbb\x9a\r\n‘ b‘\xe6\xbb\x9a‘
with open(r‘g.txt‘,mode=‘rt‘,encoding=‘utf-8‘) as f: # res1=f.readline() # res2=f.readline() # print(res2) while True: line=f.readline() if len(line) == 0: break print(line)
with open(r‘g.txt‘,mode=‘rt‘,encoding=‘utf-8‘) as f: res=f.readlines() print(res)
f.read()与f.readlines()都是将内容一次性读入内容,如果内容过大会导致内存溢出,若还想将内容全读入内存,则必须分多次读入,有两种实现方式:
# 方式一 with open(‘a.txt‘,mode=‘rt‘,encoding=‘utf-8‘) as f: for line in f: print(line) # 同一时刻只读入一行内容到内存中 # 方式二 with open(‘1.mp4‘,mode=‘rb‘) as f: while True: data=f.read(1024) # 同一时刻只读入1024个Bytes到内存中 if len(data) == 0: break print(data)
with open(‘h.txt‘,mode=‘wt‘,encoding=‘utf-8‘) as f: # f.write(‘1111\n222\n3333\n‘) # l=[‘11111\n‘,‘2222‘,‘3333‘,4444] l=[‘11111\n‘,‘2222‘,‘3333‘] # for line in l: # f.write(line) f.writelines(l)
with open(‘h.txt‘,mode=‘wt‘,encoding=‘utf-8‘) as f: l = [‘111\n‘,‘222\n‘,‘333‘,444] for line in l: f.write(line) #TypeError: write() argument must be str, not int
with open(‘h.txt‘, mode=‘wb‘) as f: l = [ ‘1111aaa1\n‘.encode(‘utf-8‘), ‘222bb2‘.encode(‘utf-8‘), ‘33eee33‘.encode(‘utf-8‘) ]
with open(‘h.txt‘, mode=‘wb‘) as f: l = [ b‘1111aaa1\n‘, b‘222bb2‘, b‘33eee33‘ ]
with open(‘h.txt‘, mode=‘wb‘) as f: l = [ bytes(‘上啊‘,encoding=‘utf-8‘), bytes(‘冲呀‘,encoding=‘utf-8‘), bytes(‘小垃圾们‘,encoding=‘utf-8‘), ]
with open(‘h.txt‘, mode=‘wt‘,encoding=‘utf-8‘) as f: f.write(‘哈‘) # f.flush()
with open(‘h.txt‘, mode=‘wt‘,encoding=‘utf-8‘) as f: print(f.readable()) print(f.writable()) print(f.encoding) print(f.name) print(f.closed)
标签:内存溢出 日志 win 找不到 exist 移动 硬盘 NPU 开头
原文地址:https://www.cnblogs.com/zhww/p/12982905.html