Demo:

file = open("test.txt", "wb")
file.write("string")

上面这段代码运行会报类型错误:TypeError: a bytes-like object is required, not 'str'

wirte方法是将一个字节缓冲区写入到目标文件中,而不支持string类型

write源码:

def write(self, *args, **kwargs): # real signature unknown
"""
Write buffer b to file, return number of bytes written. Only makes one system call, so not all of the data may be written.
The number of bytes actually written is returned. In non-blocking mode,
returns None if the write would block.
"""
pass

注:python3 将字符串写入到目标文件时,必须先将字符串转换为字节才能写入,python2可以直接将字符串写入到目标文件中

Python3 写入文件的更多相关文章

  1. python3写入文件时编码问题报错

    在字符串写入文件时,有时会因编码问题导致无法写入,可在open方法中指定encoding参数 chfile = open(filename, 'w', encoding='utf-8') 这样可解决大 ...

  2. 九九乘法表python3写入文件中

    写入文件代码如下: with open("e:\\test01.txt","w+",encoding="utf-8") as wq: for ...

  3. python3 购物车小程序,余额写入文件保存

    python3 购物车小程序,余额写入文件保存 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan goods = ( ...

  4. [Python] python3 文件操作:从键盘输入、打开关闭文件、读取写入文件、重命名与删除文件等

    1.从键盘输入 Python 2有两个内置的函数用于从标准输入读取数据,默认情况下来自键盘.这两个函数分别是:input()和raw_input(). Python 3中,不建议使用raw_input ...

  5. sublime python3中读取和写入文件时如何解决编码问题

    # -*- coding: utf-8 -*- #分析用户身份审核信息 #python 3.5 #xiaodeng #http://apistore.baidu.com/apiworks/servic ...

  6. csv.writer写入文件有多余的空行

    在用csv.writer写入文件的时候发现中间有多余的空行. 最早打开方式只是‘w’,会出现多余的空行,网上建议使用binary形式‘wb’打开可以解决问题: with open('egg2.csv' ...

  7. Python3:文件读写

    Python3:文件读写 open f = open('filename','r') # 读模式 f = open('filename','w') # 写模式 f = open('filename', ...

  8. python3中文件的读与写

    Python open() 函数用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出错误 完整语法:open(file, mode='r', buf ...

  9. python3.7 文件操作

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 文件操作 # r 只读,默认打开方式,当文件不存在时会报错 # ...

随机推荐

  1. 进程调度函数schedule()分析

    1.功能简述: 最主要作用就是 从就绪进程中选择一个优先级最高的进程来代替当前进程运行.   2.代码分析 schedule();      struct task_struct *tsk = cur ...

  2. jquery 获取radio被选中的值

    <html> <head> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"& ...

  3. springboot在eclipse实现热部署

    eclipse使用spring-tool-suite插件创建springboot项目,项目创建完成后. 选中项目,右键 Spring Tools  --> Add Boot Devtools 点 ...

  4. topcoder 的一些输入输出格式

    自从上年的11月份参加过TC的比赛后,就再也没有参加了,因为它的输入输出格式比较难接受,还有它的页面字体比较小,看得我很辛苦...藉口藉口--懒而已!不过以后我会尽量去参加的,为了提高自己的编程能力. ...

  5. PHP Framework MVC Benchmark 基准测试

    身边有朋友在用yaf框架,讨论的也声音也比较多,今天没事看鸟哥的博客,看到一篇现在PHP主流的几个框架性能对比,比较有意思,给大家分享一下! Yaf是用PHP扩展的形式写的一个PHP框架,也就是以C语 ...

  6. C++中volatile及编译器优化

    首先看一下单词"volatile"的释义: volatile [ˈvɑlətl] adj.  易变的,不稳定的; (液体或油)易挥发的; 爆炸性的; 快活的,轻快的; 下边是&qu ...

  7. [SoapUI] Read data from response , use it to update parameter

    import com.eviware.soapui.support.GroovyUtils def groovyUtils = new GroovyUtils( context ) def holde ...

  8. [Selenium] HTML5 中的 Geolocation

    在 HTML5 ,新特性 Geolocation 用于定位用户位置信息. 由于用户位置信息是敏感信息,所以需要得到用户允许后,才能让程序通过 API  获取当前用户信息.WebDriver 程序每次重 ...

  9. MySQL主从详细安装步骤

    网站: 程序在:web服务器192.168.1.100上面 数据库在:MySQL服务器192.168.1.123上面 实现目的:增加一台MySQL备份服务器(192.168.1.124),作为MySQ ...

  10. C++初学(1) 简单的加减乘除取余运算代码

    //---------------+-*/%算法---------------------------------------------------------- #include <iost ...