1、打开文件的模式主要有,r、w、a、r+、w+、a+

file = open('test.txt',mode='w',encoding='utf-8')
file.write('hello,world!')
file.close() #由此txt文件内容为hello,world!

2、r+:可读可写,根据光标所在位置开始执行。先写的话,光标在第一个位置---覆盖写,后读的时候根据光标所在位置往后读;但不管读几个字符,读过后,光标在文末

  建议:操作时,读和写分开比较好,编码时涉及到中文用utf-8

file = open('test.txt',mode='r+',encoding='utf-8')
file.write('Monday!')
content = file.read() #content = file.read(6),表示读取6个字符,但只要读过后,光标会在文末
file.close() #由此txt文件内容为:Monday!hello,world!
print(content) #打印结果为:hello,world!
file = open('test.txt',mode='r+',encoding='utf-8')
content = file.read()
file.write('Monday!')
file.close() #由此txt文件内容为:hello,world!Monday!
print(content) #打印结果为:hello,world!

3、w+:可读可写。不管w还是w+,存在文件会清空重写,不存在文件会新建文件写;

  因为会清空重写,所以不建议使用

file = open('test.txt',mode='w+',encoding='utf-8')
file.write('哮天犬!')
file.close() #由此txt文件内容为:哮天犬!

4、a:追加写。如果文件存在追加写,如果文件不存在,则新建文件写

file = open('test.txt',mode='a',encoding='utf-8')
file.write('哮天犬!')
file.close() #由此txt文件内容为:哮天犬!哮天犬!

5、读写多行操作

写多行

file = open('test.txt',mode='a',encoding='utf-8')
file.writelines(['\n二哈!','\n土狗!']) #此处的\n为换行
file.close()
文件内容:
哮天犬!
二哈!
土狗!

读多行

file = open('test.txt',mode='r',encoding='utf-8')
content = file.readlines() #读出的为列表
file.close()
print(content)
控制台输出:['哮天犬!\n', '二哈!\n', '土狗!']

总结:a:建议使用时读写操作分离,即不建议使用w+、r+、a+

   b:写的话,不建议使用w,建议使用a;读的话,使用r

      c:使用中文时,编码要使用utf-8

python--txt文件处理的更多相关文章

  1. python txt文件常用读写操作

    文件的打开的两种方式 f = open("data.txt","r") #设置文件对象 f.close() #关闭文件 #为了方便,避免忘记close掉这个文件 ...

  2. python txt文件批处理

    首先,切换文件路径到所在文件夹 然后,将txt文件内容按行读取,写入到all.txt def txtcombine(): files=glob.glob('*.txt') all = codecs.o ...

  3. python txt文件的写入和读取

    1.文件的打开 使用open () 函数 打开文件.他有两个参数,文件路径或文件名和文件的打开方式. "r" 只读模式,不能编辑和删除文件内容. "w" 写入模 ...

  4. Python txt文件读取写入字典的方法(json、eval)

    link:https://blog.csdn.net/li532331251/article/details/78203438 一.使用json转换方法 1.字典写入txt import json d ...

  5. python txt文件读写(追加、覆盖)

    (1)在lucky.txt中新增内容(覆盖:每次运行都会重新写入内容) f = "lucky.txt" a =8 with open(f,"w") as fil ...

  6. python txt文件数据转excel

    txt content: perf.txt 2018-11-12 16:48:58 time: 16:48:58 load average: 0.62, 0.54, 0.56 mosquitto CP ...

  7. 【学习总结】GirlsInAI ML-diary day-15-读/写txt文件

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day15 认识读/写txt文件 路径: 绝对路径:文件在电脑中的位置 相对路径:下面会用到 1-准备 新建一个 pytho ...

  8. python打开文件的N种姿势

    # python打开文件的N种姿势 print('[1]使用open()函数+简单for循环') f1 = open('python.txt') for line in f1: print(line. ...

  9. Python新建动态命名txt文件

    # -*- coding: utf-8 -*- import os,sys,time fname=r"D:\01-学习资料\python" def GetNowTime():#获取 ...

  10. python 查找指定内容的txt文件

    程序设计思路:1. 利用os.walk()找出所有的文件;2.利用正则找到指定后缀的文件:3.找到需要的txt文件后,通过open().readlines()读取文件中每行数据;4.读取后,保存正则匹 ...

随机推荐

  1. 微信web版接口api(转)

    安卓微信的api,个人微信开发API协议,微信 ipad sdk,微信ipad协议,微信web版接口api,微信网页版接口,微信电脑版sdk,微信开发sdk,微信开发API,微信协议,微信接口文档sd ...

  2. intent 跳转

    一.不需要返回值的跳转 Intent intent=new Intent(); intent.setClass(目前的acitivy.this, 目标activity.class); startAct ...

  3. 「JLOI2014」聪明的燕姿

    传送门 Luogu 解题思路 很容易想到直接构造合法的数,但是这显然是会T飞的. 我们需要考虑这样一件事: 对于一个数 \(n\),对其进行质因数分解: \[n=\sum_{i=1}^x p_i^{c ...

  4. 分页--pagination.js

    var pagination = function (thispage, totalpage, ulele, firstlast) { ulele.html(''); var prevCss, nex ...

  5. sass计算高度

    页面布局时,有时候需要两个div充满父div空间,设定一个div尺寸后,可以使用css计算高度设置另一个尺寸: <style> .wrap{ width:1000px; } .left{ ...

  6. upload-labs-env文件上传漏洞 11-19关

    Pass-11 源码:加上了本人的注释=.= $is_upload = false; $msg = null; if(isset($_POST['submit'])){ $ext_arr = arra ...

  7. P1076 Wifi密码

    P1076 Wifi密码 转跳点:

  8. day03-Python运维开发基础-(数据类型强转、运算符、逻辑短路、isinstance)

    1. 强制转换成容器数据类型 # ### 强制类型转换 容器类型数据 (str list tuple set ) var1 = "你好世界" var2 = ["陈博文&q ...

  9. vue 中使用 echarts 自适应问题

    echarts 自带的自适应方法  resize() 具体用法: let xxEcharts = this.$echarts.init(document.getElementById('xxx')) ...

  10. MySQL之表、列别名及各种JOIN连接详解

    MySQL在SQL中,合理的别名可以让SQL更容易以及可读性更高.别名使用as来表示,可以分为表别名和列别名,别名应该是先定义后使用才对,所以首先要了解sql的执行顺序(1) from(2) on(3 ...