使用Python读写文本文件及编写简单的文本编辑器


Posted in Python onMarch 11, 2016

学习raw_input和argv是学习读取文件的前提,你可能不能完全理解这个练习,所以认真学习并检查。如果不认真的话,很容易删除一些有用的文件。

这个练习包含两个文件,一个是运行文件ex15.py,一个是ex15_sample.txt。第二个文件不是脚本文件,只包括一些文本,如下:

This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

我们要做的就是打开这个文件,然后打印文件内容,我们不在代码中写死文件名称,因为我们如果要读取其他文件的话,就要重新修改代码,解决这个问题的办法就是使用argv和raw_input。

from sys import argv 
 
 
script, filename = argv 
 
 
txt = open(filename) 
 
 
print "Here's your file %r:" % filename 
print txt.read() 
 
 
print "Type the filename again:" 
file_again = raw_input("> ") 
 
 
txt_again = open(file_again) 
 
 
print txt_again.read()

上面的代码做了一些有意思的事情,让我们快速的分解一下:

1-3行使用argv取得文件名。第5行使用open命令,现在使用pydoc open看看这个命令的介绍。

第7行打印一行信息,但是第8行有一些新的东西。我们在txt上调用了一个方法。我们通过open方法得到一个file,这个file有一些我们可以调用的方法。使用这些方法的方法就是在file后面加一个.(点),比如txt.read(),就像是说:“嘿,执行读取命令,没有任何参数!”

剩下部分大家在加分练习中分析吧。

运行结果

root@he-desktop:~/mystuff# python ex15.py ex15_sample.txt
Here's your file 'ex15_sample.txt':

This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

Type the filename again:
> ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

下面几个文件的命令比较常用:

  • close -- 关闭文件,相当于编辑器中的File->Save
  • read -- 读取文件内容分配给一个变量
  • readline -- 读取一行内容
  • truncate -- 清空文件,小心使用这个命令
  • write(stuff) -- 写入文件。

这些是你应该知道的重要命令,只有write需要提供参数。

让我们使用这些命令实现一个简单的文本编辑器。

from sys import argv 
 
 
script, filename = argv 
 
 
print "We're going to erase %r." % filename 
print "If you don't want that, hit CTRL-C (^C)." 
print "If you do want that, hot RETURN." 
 
 
raw_input("?") 
 
 
print "Opening the file..." 
target = open(filename, 'w') 
 
 
print "Truncating the file. Goodbye!!" 
target.truncate() 
 
 
print "Now I'm going to ask you for three lines." 
 
 
line1 = raw_input("line 1: ") 
line2 = raw_input("line 2: ") 
line3 = raw_input("line 3: ") 
 
 
print "I'm going to write these to the file." 
 
 
target.write(line1) 
target.write("\n") 
target.write(line2) 
target.write("\n") 
target.write(line3) 
target.write("\n") 
 
 
print "And finally, we close it." 
target.close()

这个程序比较长,所以慢慢来,让它能运行起来。有个办法是,先写几行,运行一下,可以运行再写几行,直到都可以运行。

运行结果
你会看到两个东西,一个是程序的输出:

root@he-desktop:~/mystuff# python ex16.py test.txt
We're going to erase 'test.txt'.
If you don't want that, hit CTRL-C (^C).
If you do want that, hot RETURN.
?
Opening the file...
Truncating the file. Goodbye!!
Now I'm going to ask you for three lines.
line 1: Hi!
line 2: Welcome to my blog!
line 3: Thank you!
I'm going to write these to the file.
And finally, we close it.

还有就是你新建立的文件,打开看看吧。

Python 相关文章推荐
Python的动态重新封装的教程
Apr 11 Python
python实现周期方波信号频谱图
Jul 21 Python
python实现指定文件夹下的指定文件移动到指定位置
Sep 17 Python
python通过配置文件共享全局变量的实例
Jan 11 Python
PyCharm搭建Spark开发环境实现第一个pyspark程序
Jun 13 Python
使用Python制作表情包实现换脸功能
Jul 19 Python
python 已知三条边求三角形的角度案例
Apr 12 Python
django使用JWT保存用户登录信息
Apr 22 Python
python对execl 处理操作代码
Jun 22 Python
Python爬虫教程之利用正则表达式匹配网页内容
Dec 08 Python
matplotlib制作雷达图报错ValueError的实现
Jan 05 Python
python中pycryto实现数据加密
Apr 29 Python
简单讲解Python中的数字类型及基本的数学计算
Mar 11 #Python
详解Python中的变量及其命名和打印
Mar 11 #Python
Python基本语法经典教程
Mar 11 #Python
Python使用PIL库实现验证码图片的方法
Mar 11 #Python
Python2.x利用commands模块执行Linux shell命令
Mar 11 #Python
Python实现列表转换成字典数据结构的方法
Mar 11 #Python
python中enumerate函数遍历元素用法分析
Mar 11 #Python
You might like
详解PHP处理字符串类似indexof的方法函数
2017/06/11 PHP
PHP crc32()函数讲解
2019/02/14 PHP
ImageFlow可鼠标控制图片滚动
2008/01/30 Javascript
jquery下将选择的checkbox的id组成字符串的方法
2010/11/28 Javascript
js confirm()方法的使用方法实例
2013/07/13 Javascript
js和as的稳定传值问题解决
2013/07/14 Javascript
jqGrid读取选择的多行的某个属性代码
2014/05/18 Javascript
浅析Node.js查找字符串功能
2014/09/03 Javascript
JS的数组迭代方法
2015/02/05 Javascript
分享9点个人认为比较重要的javascript 编程技巧
2015/04/27 Javascript
javascript日期格式化方法汇总
2015/10/04 Javascript
浅谈JQuery+ajax+jsonp 跨域访问
2016/06/25 Javascript
浅谈在vue项目中如何定义全局变量和全局函数
2017/10/24 Javascript
微信小程序表单验证form提交错误提示效果
2020/06/19 Javascript
JS函数进阶之prototy用法实例分析
2020/01/15 Javascript
jQuery 函数实例分析【函数声明、函数表达式、匿名函数等】
2020/05/19 jQuery
[49:40]2018DOTA2亚洲邀请赛小组赛 A组加赛 TNC vs Newbee
2018/04/03 DOTA
Python中自定义函数的教程
2015/04/27 Python
sublime text 3配置使用python操作方法
2017/06/11 Python
Python编程实现二分法和牛顿迭代法求平方根代码
2017/12/04 Python
TensorFlow深度学习之卷积神经网络CNN
2018/03/09 Python
python PIL和CV对 图片的读取,显示,裁剪,保存实现方法
2019/08/07 Python
python装饰器练习题及答案
2019/11/01 Python
Python如何读写字节数据
2020/08/05 Python
协程Python 中实现多任务耗资源最小的方式
2020/10/19 Python
python获取命令行参数实例方法讲解
2020/11/02 Python
可以随进度显示不同颜色的css3进度条分享
2014/04/11 HTML / CSS
应届毕业生通用的自荐书范文
2014/02/07 职场文书
军训学生自我鉴定
2014/02/12 职场文书
大学优秀班主任事迹材料
2014/05/02 职场文书
优秀党员事迹材料
2014/12/18 职场文书
电气工程师岗位职责
2015/02/12 职场文书
2015年教师节贺卡寄语
2015/03/24 职场文书
销售人员管理制度
2015/08/06 职场文书
《鲸》教学反思
2016/02/23 职场文书
CSS 鼠标点击拖拽效果的实现代码
2022/12/24 HTML / CSS