.txt文件常用读写操作

本文通过一个实例来介绍读写txt文件的各种常用操作,问题修改自coursera上南京大学的课程:用Python玩转数据。

直接进入正题,考虑下面为练习读写txt文件的各种操作而设计的一个具体问题

问题如下:

(1) 在任意位置创建一个.py文件,如'D:/编程练习/python练习/Week2_02.py'

(2) 在D盘下创建一个文件Blowing in the wind.txt,即‘D:\Blowing in the wind.txt’

其内容是:

How many roads must a man walk down

Before they call him a man

How many seas must a white dove sail

Before she sleeps in the sand

How many times must the cannon balls fly

Before they're forever banned

The answer my friend is blowing in the wind

The answer is blowing in the wind

(3) 在文件头部插入歌名“Blowin’ in the wind”

(4) 在歌名后插入歌手名“Bob Dylan”

(5) 在文件末尾加上字符串“1962 by Warner Bros. Inc.”

(6) 在屏幕上打印文件内容,最好加上自己的设计

(7) 以上每一个要求均作为一个独立的步骤进行,即每次都重新打开并操作文件

--------------------------------------------------------------------------------------------------------------------

程序代码如下:

import os
#Python的os模块提供了执行文件和目录处理操作的函数,例如重命名和删除文件。
os.chdir('D:\\') #更改目录 #-------------------------------------------------------------------------
#创建一个文件,将歌词写入
f1=open(r'Blowing in the wind.txt','w')
f1.write('How many roads must a man walk down \n')
f1.write('Before they call him a man \n')
f1.write('How many seas must a white dove sail \n')
f1.write('Before she sleeps in the sand \n')
f1.write('How many times must the cannon balls fly \n')
f1.write('Before they\'re forever banned \n')
f1.write('The answer my friend is blowing in the wind \n')
f1.write('The answer is blowing in the wind\n')
f1.close()#文件使用后记得关闭#--------------------------------------------------------------------------
#在文件头部插入歌曲名
f2=open(r'Blowing in the wind.txt','r+')#mode参数不能用'w+',这会清空原内容
lyrics =f2.readlines()
lyrics.insert(0,'Blowin\'in the wind\n')#在第一行添加歌曲名
f2.seek(0,0)#将文件指针移动到文件开头处
f2.writelines(lyrics)
f2.close()#这是一种错误的写法,将歌词的第一行抹去了一部分
#f2=open(r'Blowing in the wind.txt','r+')
#f2.seek(0,0)#将文件指针移动到文件开头处
#f2.write('Blowin’ in the wind\n')
#f2.close()#--------------------------------------------------------------------------
#在歌名后插入歌手名(实现同上一步)
f3=open(r'Blowing in the wind.txt','r+')#mode参数不能用'w+',这会清空原内容
lyrics =f3.readlines()
lyrics.insert(1,'——Bob Dylan\n')#在第一行添加歌手名
f3.seek(0,0)#将文件指针移动到文件开头处
f3.writelines(lyrics)
f3.close()#--------------------------------------------------------------------------
#在文件末尾加上字符串“1962 by Warner Bros. Inc.”
f4=open(r'Blowing in the wind.txt','a')#mode参数选择'a',代表追加模式.
f4.write('1962 by Warner Bros. Inc.')#追加模式下,可以直接向文件末尾write内容
f4.close()#--------------------------------------------------------------------------
#在屏幕上打印文件内容(最好加一些自己的格式设计)
f5=open(r'Blowing in the wind.txt','r')
article =f5.readlines()#读取文件内容
#按照自己的方式显示
for i in range(0,len(article)):if 1<i<len(article)-1:print('\t'+article[i])else:print('\t\t'+article[i])
f5.close()

运行后.txt文件中的内容为:

屏幕上的输出为:

Python小练习1:.txt文件常用读写操作相关推荐

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

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

  2. python读取txt文件代码-Python txt文件常用读写操作代码实例

    python读取txt文件 #方式一: file = r'D: est.txt' with open(file, 'rb+') as f: #可读可写二进制,文件若不存在就创建 data = f.re ...

  3. python文件读写方法手机_python读取文件—txt文件常用读写操作

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

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

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

  5. python 数组写txt_python txt文件常见读写操作

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

  6. Python随笔:对 txt 文件进行读写,清除,删除操作

    Python随笔:对 txt 文件进行读写,清除内容,删除操作 文章目录 Python随笔:对 txt 文件进行读写,清除内容,删除操作 1.文件读写 1.1 读写文件前打开文件的两个方法 1.2 写 ...

  7. MFC下txt文件的读写操作

    详细说明请看如下链接: https://blog.csdn.net/u012987386/article/details/71089178 头文件中包含: #include <locale> ...

  8. c++ txt文件的读写及乱码问题解决

    在编程中,我们经常需要对txt文件进行读写操作,有时候由于编解码问题,txt读写会出现乱码问题.下面介绍一种基于ofstream和ifstream的txt文件读写方法,并介绍txt文件读写的乱码解决方 ...

  9. python不同数据的读入_python读写不同编码txt文件_python读写txt文件

    python读写不同编码txt文件_python读写txt文件 以后整理规范 [python] view plaincopy import os import codecs filenames=os. ...

最新文章

  1. 在IDEA 中为Maven 配置阿里云镜像源
  2. 硬盘最多能分几个区?
  3. mysql dump 查看器_mysql备份之mysqldump工具
  4. 十三、深入Java的Scanner类
  5. 把 SAP UI5 应用部署到 SAP Kyma
  6. -code vs 1474 十进制转m进制
  7. 论 js中:(class、id)出乎意料的优先级?- 案例篇
  8. MFC中常见控件的操作
  9. 文件选择器看这个就够了—轻量级、支持多种文件类型
  10. 实习成长之路:MySQL十一:为什么我的MySQL会“抖”一下?
  11. SAP GUI 770下载
  12. 计划超越苹果!诺基亚在印度推出笔记本电脑
  13. 音频,视频合并软件——ffmpeg下载及使用
  14. Android原生Switch禁止滑动实现
  15. 6个理由告诉你为什么要用NAS
  16. Android---Toolbar
  17. opencv(十):vs2019+opencv4.1+ C++配置
  18. vue 组件数据共享_Vue共享组件
  19. 2021年安全生产模拟考试(全国特种作业操作证焊工作业-压力焊模拟考试题库一)
  20. termux python教程_Termux基础教程

热门文章

  1. day-17 包与模块
  2. BZOJ2330 SCOI2011糖果
  3. Android技巧分享——Android开发超好用工具吐血推荐 转载
  4. Node.js npm 详解
  5. 关于c#静态构造函数
  6. 在linux下添加路由
  7. C++读取INI文件
  8. Silverlight中全屏处理
  9. 转:SQL SERVER中一些常见性能问题的总结
  10. 3D 鼠标跟随脚本详解