Python3 I/O(数据流 模块)

来源:互联网 发布:java连接mongodb 编辑:程序博客网 时间:2024/06/06 01:00

原文: http://blog.csdn.net/Rozol/article/details/71087283


# #coding=utf-8# io.py I/O# I/O三种主要类型:文本I/O, 二进制I/O, 原始I/O# 流对象具有的能力:1. 读写, 2.任意随机存取, 3.顺序存取(套接字/管道)# 字符编码: Unicode(16位[2字节],全球字符) / ASCII(8位[1字节],数字字母) / utf-8(英文8位,中文24位[3字节])# 转码: utf-8 ==decode("utf-8")==> Unicode ==encode("gbk")==> gbkfilepath = "./temp/file.txt";def demo():    # 打开 (写时文件不存在将自动创建)    f = open(filepath, "w+")    # 写    f.write("Her beauty is beyond words!\r\n")    # 刷新    f.flush()    # 设置指针偏移量    f.seek(0)    # 读    content = f.read() # 读取数据(默认全部)    print(content)    # 关闭    f.close()def funs():    # io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) # 内置函数open()的别名    # file: 模式为写入文件时,文件不存在将自动创建    # mode: r(只读,指针在开头) / w(只写) / a(追加,指针在末尾) / rb(只读0b) / wb(只写0b) / ab(追加0b) / r+(读) / w+(写) / a+(追加) / rb+ / wb+ / ab+    # buffering=0 为无缓冲I/O    # --- 文本 I/O ---    f = open("file.txt", "r", encoding="utf-8")    f = io.StringIO("Her beauty is beyond words!") # 内存中的文本流    # --- 二进制I/O (缓冲I/O) ---    f = open("file.jpg", "rb")    f = io.BytesIO(b"Her beauty is beyond words!: \x05\x02\x00")    # --- Raw I/O (无缓冲I/O) ---    f = open("file.jpg", "rb", buffering=0)    # IO    num = io.DEFAULT_BUFFER_SIZE # 默认缓冲区大小    try:        pass    except io.UnsupportedOperation: # 不支持操作        pass    except io.BlockingIOError: # 公共异常        pass    # --- IOBase (所有I/O基类) ---    # 描述信息    num = f.fileno() # 描述流(整数)[底层文件描述符]    # 写入    f.writelines(["abc","def"]) # 写入多行, 不添加行分隔符    # truncate(size=None) // 截断文件 (size:None(从0到当前指针位置); < filesize(从0到指定size位置); > filesize(扩展部分用0填充))    num = f.truncate(1000)    # 读取    data = f.readline(size=-1) # 读取一行, size限制读取字节 (二进制始终:b'\n' 文本:行终止符)    data = f.readlines(hint=-1) # 读取多行, hint限制读取行数    # 指针    # seek(offset[, whence]) // 设置指针偏移量, whence:0:开头(默认);1:当前位置;2:末尾 (注意:只有0b方式打开才能用1,2)    num = f.seek(5, 0)    num = f.tell() # 当前指针位置    # 刷新与关闭    f.flush() # 刷新    f.close() # 刷新并关闭数据流    # 判断    boolean = f.closed # 数据流是否已经关闭    boolean = f.isatty() # 是否是交互式(连接到终端)    boolean = f.readable() # 是否可读    boolean = f.writable() # 是否可写    boolean = seekable() # 是否支持随机访问    # --- RawIOBase (原始二进制I/O的基类, 继承IOBase) ---    # read(size=-1) // 读取字节 size限制读取字节数,未指定readall()    f.read()    f.readall() # 读取所有字节    # --- 文本 I/O (继承IOBase) ---    strs = f.encoding # 字符集    strs = f.errors # 错误设置    # 写入    f.write("string") # 写入字符串     # 读取(同上)    # read(size=-1) // 读取字节 size限制读取字节数,未指定readall()    f.read()    f.readall() # 读取所有字节# === 文件Copy案例 ===def filecopy(filepath):    # 方式一    with open(filepath, "rb") as oldfile, open(filepath + ".back", "wb") as newfile:        for line in oldfile: # 读取            newfile.write(line) # 写入    # 方式二    with open(filepath, "rb") as oldfile, open(filepath + ".back", "wb") as newfile:    data = oldfile.read(1024)    while data:        newfile.write(data)        data = oldfile.read(1024)if __name__ == "__main__":    demo()    funs()    filecopy(filepath)


原创粉丝点击