python笔记之文件操作
import os old_name = 'log' new_name = 'new_log' os.rename(old_name, new_name)
import os os.remove("new_log")
with open('小护士班主任',encoding='utf-8') as f,open('小护士班主任.bak','w',encoding='utf-8') as f2: for line in f: if '星儿' in line: #班主任:星儿 line = line.replace('星儿','啊娇') #写文件 f2.write(line) #小护士:金老板 import os os.remove('小护士班主任') #删除文件 os.rename('小护士班主任.bak','小护士班主任') #重命名文件
打开文件 open('路径','打开方式','指定编码方式') 打开方式 r w a r+ w+ a+ b r+ 打开文件直接写 和读完再写 编码方式 —— utf-8 操作文件 读 read 一次性读 readlines 一次性读 readline 一行一行读 不知道在哪儿结束 视频 图片 rb bytes 按照字节读 for循环 —— 最好!!! 写 write 光标 —— 文件指针 seek _ 指定光标移动到某个位置 tell _ 获取光标当前的位置 truncate _ 截取文件 关闭文件 close
f = open("log",'r',encoding='utf-8') #打开文件 ret = f.read() print(ret) f.close() #关闭文件
file_name = "1.jpg" f = open(file_name, 'rb') ret = f.read() print(ret) f.close()
f = open('log', 'r', encoding='utf-8') content = f.read() print(content) f.close
f = open('log', 'r', encoding='utf-8') f.write('hello') f.close() Traceback (most recent call last): File "G:\very importent\Sublime Text3\Data\Packages\User\sublime\py\3.py", line 56, in <module> f.write('hello') io.UnsupportedOperation: not writable
f = open('log', 'rb') content = f.read() print(content) print(content.decode('utf-8')) f.close()
f = open('log', 'r+', encoding='utf-8') print(f.read()) f.write('hello') f.seek(0) print(f.read()) f.close()
r+b模式可读可写
f = open('log', 'r+b') print(f.read()) f.write('我不好'.encode('utf-8')) f.seek(0) print(f.read()) f.close()
f = open('log',mode='w',encoding='utf-8') f.write('骑兵步兵') f.close()
f = open('log',mode='wb') f.write('附近看到类似纠纷'.encode('utf-8')) f.close()
f = open('log', mode='w+', encoding='utf-8') f.write('aaa') f.seek(0) print(f.read()) f.close()
f = open('log', mode='w+b') f.write('你呀的'.encode('utf-8')) f.seek(0) print(f.read()) print(f.read().decode('utf-8')) f.close()
f = open('b.txt','a',encoding='utf-8') f.write('hello\r\nnihao ') f.close()
f = open('log', 'a', encoding='utf-8') print(f.read()) f.close() Traceback (most recent call last): File "G:\very importent\Sublime Text3\Data\Packages\User\sublime\py\3.py", line 60, in <module> print(f.read()) io.UnsupportedOperation: not readable
f = open('log', 'a+', encoding='utf-8') f.write("nihao") f.seek(0) print(f.read()) f.close()
f = open('log', mode='r', encoding='utf-8') print(f.read()) f.close() with open('log', mode='r', encoding='utf-8') as f: print(f.read())
可以读 但是为什么没数据,文件读写位置在 最末尾,无法获取数据
f = open('b.txt','a+',encoding='utf-8') ret = f.read() print(ret) f.close()
f = open('b.txt','a+',encoding='utf-8') print(f.tell()) #tell告诉当前光标位置 f.seek(10,0) #seek移动光标位置,第一个参数是 移动多少位 第2个参数 从什么地方开始 0: 文件开头 1当前位置 2结尾,移动到第10个字符 print(f.tell()) # ret = f.read() print(ret) f.close() f = open('log',mode='a+',encoding='utf-8') f.write('佳琪') count = f.tell() f.seek(count-9) print(f.read(2)) f.close() with open('a.txt','r',encoding='utf-8') as f: f.readable() # 是否可读 #line = f.readline() # 一行一行的读 lines = f.readlines() # 读所有行的数据,返回一个列表 for i in lines: print(i) 大文件读写 方式一:循环 方式二:for i in f.readlines:
lines = ["你好eric\n", '你好潮装\n', '我爱python\n'] with open('多行写入.c', 'w+', encoding='utf-8') as f: f.writelines(lines) errors='ignore' 编码不一致不报错 strict 报错。 with open('多行写入.c', 'r+', encoding='gbk', errors='ignore') as f: print(f.read().encode('utf-8')) 方式1 : 循环尝试 【'gbk', 'utf-8', 'gbk2312'】 方式2 : chardet import chardet with open('a.txt', 'rb') as f: data = f.read() print(chardet.detect(data)) with open('d:/sound.mp3', 'rb') as f: data = f.read() with open('b.mp3', 'wb') as f1: f1.write(data)
#1 f = open('d:\模特主妇护士班主任.txt',mode='r',encoding='UTF-8') content = f.read() print(content) f.close() #2 f = open('模特主妇护士班主任',mode='r+',encoding='utf-8') content = f.read() f.write('abc') f.close() #3 f = open('log',mode='w',encoding='utf-8') f.write('abc') f.close() #4 f = open('log1', mode='a', encoding='utf-8') f.write('abc') f.close()
f = open('模特主妇护士班主任',mode='rb',) content = f.read() print(content) f.close() f = open('log',mode='r+b') print(f.read()) f.write('大猛,小孟'.encode('utf-8')) f.close() file_name = "1.jpg" f = open(file_name, 'rb') ret = f.read() print(ret) f1 = open('2.jpg', 'wb') f1.write(ret) f1.close()
username = input('请输入你要注册的用户名:') password = input('请输入你要注册的密码:') with open('list_of_info',mode='w',encoding='utf-8') as f: f.write('{}\n{}'.format(username,password)) print('恭喜您,注册成功') lis = [] i = 0 while i < 3: usn = input('请输入你的用户名:') pwd = input('请输入你的密码:') with open('list_of_info',mode='r+',encoding='utf-8') as f1: for line in f1: lis.append(line) if usn == lis[0].strip() and pwd == lis[1].strip(): print('登录成功') break else:print('账号和密码错误') i+=1