with的使用:
with open('filename','rwa') as x:
下一步操作 # 一个缩进块
文件操作b模式
#!/usr/bin/env python with open('test11.py','rb') as f: data = f.read() print(data) print(data.decode('utf8'))
# #'字符串'---------encode---------》bytes
# #bytes---------decode---------》'字符串'
# p = open('test22.py','wb') # p.write(bytes('111 ',encoding='utf-8')) # p.write('中文'.encode('utf8')) with open('test22.py','ab') as o: o.write('应'.encode('utf8'))
# f.flush() #讲文件内容从内存刷到硬盘 # # f.closed #文件如果关闭则返回True # # f.encoding #查看使用open打开文件的编码 # f.tell() #查看文件处理当前的光标位置 # # f.seek(3) #从开头开始算,将光标移动到第三个字节 # f.truncate(10) #从开头开始算,将文件只保留从0-10个字节的内容,文件必须以写方式打开,但是w和w+除外