python 文本模式读写文件时 不应使用 os.linesep 简介
os.linesep官方文档
The string used to separate (or, rather, terminate) lines on the current platform. This may be a single character, such as '\n' for POSIX, or multiple characters, for example, '\r\n' for Windows. Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single '\n' instead, on all platforms.
如上,os.linesep是用来分割文件的每一行(即文件结束符),由于在不同操作系统下文件结束符不一定相同,所以os.linesep是跨平台的文件描述符,比如在Windows平台上是'\r\n',在Linux平台上则是'\n'
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import os
>>> os.linesep
'\r\n'
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.linesep
'\n'
但是以open
默认的文本模式读写时,'\n'会被自动转换成'\r\n'。在Windows平台实验如下
>>> with open(r'D:\test.txt', 'w') as f:f.write(os.linesep)2
>>> with open(r'D:\test.txt', 'rb') as f:f.read()b'\r\r\n'
本来是要写入结束符'\r\n',结果由于python自动把'\n'替换成'\r\n'导致写入的是'\r\n\n'。因此按照官方的建议,此时使用'\n'代替os.linesep即可。
不过在二进制模式下,为文本文件添加换行符的操作用os.linesep来实现跨平台更好。
>>> with open(r'D:\test.txt', 'wb') as f:f.write(os.linesep.encode())2
>>> with open(r'D:\test.txt', 'rb') as f:f.read()b'\r\n'
参考资料
https://stackoverflow.com/questions/21636213/why-you-shouldnt-use-os-linesep-when-editing-on-text-mode
python 文本模式读写文件时 不应使用 os.linesep 简介相关推荐
- oracle 文件写 n r,文本模式读写文件中\r和\n的问题
DOS文件(windows格式文件)中,按一下ENTER就是输入了\r\n,这就是回车换行 MAC文件里,用\r换行 UNIX文件里,用\n换行 自己尝试了一些\r \n的组合,归纳了下规律: win ...
- 文本模式读写文件中\r和\n的问题
原文地址:http://blog.sina.com.cn/s/blog_65db99840100kidc.html DOS文件(windows格式文件)中,按一下ENTER就是输入了\r\n,这就是回 ...
- Python随笔:进行读写文件时,在字符串前加 r,u,b,f 的含义
Python随笔:进行读写文件时,在字符串前加 r,u,b,f 的含义 文章目录 Python随笔:进行读写文件时,在字符串前加 r,u,b,f 的含义 1.r'xxx' 的含义 2.u'xxx' 的 ...
- python open写入_【Python】使用with open读写文件时,文件不存在没有自动创建
with open(output_filename, "wb") as f: f.write(html_request.content) 我用这个方法读写文件时,却报错:IOErr ...
- python读取二进制文件_python读取二进制流 Python二进制方式读写文件 - 电脑常识 - 服务器之家...
python读取二进制流 Python二进制方式读写文件 发布时间:2017-03-18 来源:服务器之家 这两天用Python解析ELF二进制文件,遇到了不少问题,总结整理如下,以便加深对Pytho ...
- Python中open读写文件操作
Python内置了读写文件的函数open() 1.读文件(使用内置的open()函数) f = open('/Users/michael/test.txt', 'r') 'r' 表示读,我可以可以利用 ...
- python写文件读文件-Python 实例:读写文件
原标题:Python 实例:读写文件 读写文件是最常见的IO操作.内置了读写文件的函数,用法和的读写文件非常类似.在磁盘上读写文件的功能都是由提供的,现代不允许普通的程序直接操作磁盘,所以,读写文件就 ...
- python存成csv文件时中文乱码_Python在向CSV文件写中文时乱码的处理办法
前言 python2最大的坑在于中文编码问题,遇到中文报错首先加u,再各种encode.decode. 当list.tuple.dict里面有中文时,打印出来的是Unicode编码,这个是无解的. 对 ...
- 解决python 保存json到文件时 中文显示16进制编码的问题
解决python 保存json到文件时 中文显示16进制编码的问题 参考文章: (1)解决python 保存json到文件时 中文显示16进制编码的问题 (2)https://www.cnblogs. ...
最新文章
- 机器学习_生成式模型与判别式模型
- 【转】DCT变换的透彻解析
- python class函数报错_Python multiprocess pool模块报错pickling error问题解决方法分析
- spark-sql建表语句限制_第三篇|Spark SQL编程指南
- android 开机动画 渐变,[Parallax Animation]实现知乎 Android 客户端启动页视差滚动效果...
- SpringBoot2.1.5 (32)--- SpringBoot整合 Freemaker 模板引擎
- 2018.09.29 bzoj3885: Cow Rectangles(悬线法+二分)
- 论文阅读笔记:《Contextual String Embeddings for Sequence Labeling》
- 计算机四年级测试题目,小学四年级下册数学总复习测试题(人教版)
- thinkcmf apache rewrite文件
- 2020年 2月 省市区sql(一)
- 1998-2018 TOM邮箱20年发展
- 文档服务器 件排名,十大服务器排名
- Android计步器的实现(1)
- 【第3篇】人工智能(AI)语音测试原理和实践
- 升级到Abp 7.0后脚本问题的临时解决方案
- jQuery拆分字符串 split()并且遍历$.each()
- Linux三剑客awk之行和列
- semantic navigation 目标驱动的视觉语义导航(二)
- im即时通讯开发之后台应用保活、消息推送的噩梦
热门文章
- 用dw中html设置背景,Dreamweaver 教程-CSS背景属性(background)
- Tungsten Fabric SDN — 报文转发流程
- 急招 Golang 熟手,来做 5G 事业。
- KVM — 与 QEMU 和 Libvirt 的关系
- NR 5G 网络切片
- Netty+SpringBoot+FastDFS+Html5实现聊天App
- 20172303 2018-2019-1《程序设计与数据结构》课程总结
- SSH方式连接Git服务器需要注意的地方
- 基于虚拟帐号的邮件系统(extmail)上
- Linux环境下进入MySQL环境报权限问题:Access denied for user 'root@localhost' (using password:YSE)...