使用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 django实现简单的邮件系统发送邮件功能
Jul 14 Python
Python编程使用tkinter模块实现计算器软件完整代码示例
Nov 29 Python
Python编程实现从字典中提取子集的方法分析
Feb 09 Python
Python实现Dijkstra算法
Oct 17 Python
在django view中给form传入参数的例子
Jul 19 Python
详解python中index()、find()方法
Aug 29 Python
python异常处理和日志处理方式
Dec 24 Python
基于django2.2连oracle11g解决版本冲突的问题
Jul 02 Python
Python识别验证码的实现示例
Sep 30 Python
Python爬虫Scrapy框架CrawlSpider原理及使用案例
Nov 20 Python
Python基于template实现字符串替换
Nov 27 Python
Python爬虫自动化爬取b站实时弹幕实例方法
Jan 26 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
抓取YAHOO股票报价的类
2009/05/15 PHP
php实现jQuery扩展函数
2009/10/30 PHP
ExtJS与PHP、MySQL实现存储的方法
2010/04/02 PHP
基于php流程控制语句和循环控制语句(讲解)
2017/10/23 PHP
DOM下的节点属性和操作小结
2009/05/14 Javascript
javascript操作cookie_获取与修改代码
2009/05/21 Javascript
jquery验证手机号码、邮箱格式是否正确示例代码
2013/07/28 Javascript
jquery的ajax跨域请求原理和示例
2014/05/08 Javascript
JavaScript中的依赖注入详解
2015/03/18 Javascript
javascript框架设计之浏览器的嗅探和特征侦测
2015/06/23 Javascript
Vue组件BootPage实现简单的分页功能
2016/09/12 Javascript
bootstrap表单按回车会自动刷新页面的解决办法
2017/03/08 Javascript
基于JavaScript实现活动倒计时效果
2017/04/20 Javascript
jquery.guide.js新版上线操作向导镂空提示jQuery插件(推荐)
2017/05/20 jQuery
JS动态添加的div点击跳转到另一页面实现代码
2017/09/30 Javascript
原生JS实现逼真的图片3D旋转效果详解
2019/02/16 Javascript
js 实现ajax发送步骤过程详解
2019/07/25 Javascript
JS实现音乐钢琴特效
2020/01/06 Javascript
使用python将mdb数据库文件导入postgresql数据库示例
2014/02/17 Python
linux 下实现python多版本安装实践
2014/11/18 Python
python取代netcat过程分析
2018/02/10 Python
Python实现求一个集合所有子集的示例
2018/05/04 Python
Python数学形态学实例分析
2019/09/06 Python
python SocketServer源码深入解读
2019/09/17 Python
Python语法垃圾回收机制原理解析
2020/03/25 Python
Django contrib auth authenticate函数源码解析
2020/11/12 Python
python中的unittest框架实例详解
2021/02/05 Python
Java面试题及答案
2012/09/08 面试题
学生干部学习的自我评价
2014/02/18 职场文书
自考毕业自我鉴定
2014/03/18 职场文书
导航工程专业自荐信
2014/09/02 职场文书
县委党的群众路线教育实践活动工作情况报告
2014/10/25 职场文书
党员违纪检讨书
2015/05/05 职场文书
Pyhton模块和包相关知识总结
2021/05/12 Python
从np.random.normal()到正态分布的拟合操作
2021/06/02 Python
Python基础教程,Python入门教程(超详细)
2021/06/24 Python