使用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实现简单的计时器功能函数
Mar 14 Python
Python中使用copy模块实现列表(list)拷贝
Apr 14 Python
Python实现读取文件最后n行的方法
Feb 23 Python
Python 模拟购物车的实例讲解
Sep 11 Python
Python实现常见的回文字符串算法
Nov 14 Python
python画图系列之个性化显示x轴区段文字的实例
Dec 13 Python
pyqt5实现按钮添加背景图片以及背景图片的切换方法
Jun 13 Python
基于python实现获取网页图片过程解析
May 11 Python
Selenium alert 弹窗处理的示例代码
Aug 06 Python
Python获取江苏疫情实时数据及爬虫分析
Aug 02 Python
Elasticsearch 数据类型及管理
Apr 19 Python
Python通用验证码识别OCR库ddddocr的安装使用教程
Jul 07 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 的几个配置文件函数
2006/12/21 PHP
php递归列出所有文件和目录的代码
2008/09/10 PHP
PHP模板引擎Smarty内建函数foreach,foreachelse用法分析
2016/04/11 PHP
PHP实现的登录页面信息提示功能示例
2017/07/24 PHP
简明json介绍
2008/09/28 Javascript
jquery lazyload延迟加载技术的实现原理分析
2011/01/24 Javascript
用jQuery与JSONP轻松解决跨域访问的问题
2014/02/04 Javascript
jQuery过滤选择器用法分析
2015/02/10 Javascript
JS模拟实现方法重载示例
2016/08/03 Javascript
不使用script导入js文件的几种方法
2016/10/27 Javascript
微信小程序 template模板详解及实例
2017/02/21 Javascript
详解webpack分包及异步加载套路
2017/06/29 Javascript
vue之浏览器存储方法封装实例
2018/03/15 Javascript
VUE解决微信签名及SPA微信invalid signature问题(完美处理)
2019/03/29 Javascript
vue 源码解析之虚拟Dom-render
2019/08/26 Javascript
[01:31]完美与DOTA2历程
2014/07/31 DOTA
[02:07]DOTA2新英雄展现中国元素,完美“圣典”亮相央视
2016/12/19 DOTA
[48:48]VGJ.T vs Liquid 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
pymongo实现控制mongodb中数字字段做加法的方法
2015/03/26 Python
Python脚本暴力破解栅栏密码
2015/10/19 Python
解决python使用open打开文件中文乱码的问题
2017/12/29 Python
Python编程中类与类的关系详解
2019/08/08 Python
Python 音频生成器的实现示例
2019/12/24 Python
python发qq消息轰炸虐狗好友思路详解(完整代码)
2020/02/15 Python
pycharm + django跨域无提示的解决方法
2020/12/06 Python
浅谈HTML5新增及移除的元素
2016/06/27 HTML / CSS
阿里云:Aliyun.com
2017/02/15 全球购物
单身联谊活动方案
2014/01/29 职场文书
《槐乡五月》教学反思
2014/04/25 职场文书
村干部培训方案
2014/05/02 职场文书
2014年自愿离婚协议书
2014/10/10 职场文书
考试没考好检讨书
2015/05/06 职场文书
读《方与圆》有感:交友方圆有度
2020/01/14 职场文书
详解Python 3.10 中的新功能和变化
2021/04/28 Python
React更新渲染原理深入分析
2022/12/24 Javascript