使用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 相关文章推荐
Windows下用py2exe将Python程序打包成exe程序的教程
Apr 08 Python
在Python中用keys()方法返回字典键的教程
May 21 Python
python线程、进程和协程详解
Jul 19 Python
Python两个内置函数 locals 和globals(学习笔记)
Aug 28 Python
Python中Django 后台自定义表单控件
Mar 28 Python
Python3中类、模块、错误与异常、文件的简易教程
Nov 20 Python
Python2和Python3的共存和切换使用
Apr 12 Python
Python如何使用PIL Image制作GIF图片
May 16 Python
pycharm实现print输出保存到txt文件
Jun 01 Python
python GUI模拟实现计算器
Jun 22 Python
python使用建议与技巧分享(一)
Aug 17 Python
Python 统计序列中元素的出现频度
Apr 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
地球防卫队:陪着奥特曼打小怪兽的人类力量 那些经典队服
2020/03/08 日漫
PHP中上传大体积文件时需要的设置
2006/10/09 PHP
一些 PHP 管理系统程序中的后门
2009/08/05 PHP
PHP的可变变量名的使用方法分享
2012/02/05 PHP
修改php.ini不生效问题解决方法(上传大于8M的文件)
2013/06/14 PHP
php实现贪吃蛇小游戏
2016/07/26 PHP
json 定义
2008/06/10 Javascript
js判断浏览器类型的方法
2013/08/07 Javascript
js 上下左右键控制焦点(示例代码)
2013/12/14 Javascript
jQuery中end()方法用法实例
2015/01/08 Javascript
JavaScript基本语法讲解
2015/06/03 Javascript
JS+CSS实现仿雅虎另类滑动门切换效果
2015/10/13 Javascript
教你用十行node.js代码读取docx的文本
2017/03/08 Javascript
深入理解JavaScript继承的多种方式和优缺点
2017/05/12 Javascript
微信小程序wx:for和wx:for-item的用法详解
2018/04/01 Javascript
Angular数据绑定机制原理
2018/04/17 Javascript
微信小程序picker组件关于objectArray数据类型的绑定方法
2019/03/13 Javascript
Vue.js路由实现选项卡简单实例
2019/07/24 Javascript
Vue 刷新当前路由的实现代码
2019/09/26 Javascript
微信小程序图片右边加两行文字的代码
2020/04/23 Javascript
[27:53]2014 DOTA2华西杯精英邀请赛 5 24 NewBee VS iG
2014/05/26 DOTA
[01:04:39]OG vs Mineski 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
Python字典,函数,全局变量代码解析
2017/12/18 Python
Python实现语音识别和语音合成功能
2019/09/20 Python
用python实现一个简单的验证码
2020/12/09 Python
英国最大的海报商店:GB Posters
2018/03/20 全球购物
Tiqets英国:智能手机上的文化和娱乐门票
2019/07/10 全球购物
J2EE模式面试题
2016/10/11 面试题
《雷雨》教学反思
2014/02/20 职场文书
莫言诺贝尔获奖演讲稿
2014/05/21 职场文书
七夕情人节促销方案
2014/06/07 职场文书
2014年教师节座谈会发言稿
2014/09/10 职场文书
授权委托书样本
2014/09/25 职场文书
晋江市委常委班子四风问题整改工作方案
2014/10/26 职场文书
总结高并发下Nginx性能如何优化
2021/11/01 Servers
俄罗斯十大城市人口排名,第三首都仅排第六,第二是北方首都
2022/03/20 杂记