• 技术文章 >Python技术 >Python基础教程

    python操作文件模式的介绍

    小妮浅浅小妮浅浅2021-09-08 09:29:49原创2930

    1、只读模式(只读数据r)。

    f = open('my_file.txt', 'rb')
    f_bytes = f.read()
    print(f_bytes)
    print(f_bytes.decode('utf-8'))
    f.close()

    2、只写模式(重写数据w),以二进制的方式写入数据或只写模式追加数据。

    write_file = open('demo1.jpg', mode='wb')
    read_file = open('demo.jpg', mode='rb')
    write_file.write(read_file.read())
    read_file.close()
    write_file.close()

    3、读写模式(追加数据a),如果不想原有数据被覆盖可以使用追加写入数据的方式。

    read_write_file = open('append_file.txt', mode='r+', encoding='utf-8')
    print(read_write_file.read())
    read_write_file.write('\n努力向上,天天向上')
    print(read_write_file.read())
    read_write_file.close()

    以上就是python操作文件模式的介绍,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python操作文件
    品易云
    上一篇:python列表有什么特点 下一篇:Python with as如何使用

    相关文章推荐

    • python文件打开闪退要怎么处理?• Python文件路径是什么?怎么写?• python文件路径的操作• python文件路径的组成• python文件导入相对路径• python文件的读取和写入• python文件拆分与合并的方法• python文件的三大访问方式

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网