使用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访问MySQL封装的常用类实例
Nov 11 Python
Python中操作mysql的pymysql模块详解
Sep 13 Python
Python编程之Re模块下的函数介绍
Oct 28 Python
python抓取网页中链接的静态图片
Jan 29 Python
Python网络爬虫神器PyQuery的基本使用教程
Feb 03 Python
python学生信息管理系统(初级版)
Oct 17 Python
python tkinter实现屏保程序
Jul 30 Python
django-crontab 定时执行任务方法的实现
Sep 06 Python
使用Python paramiko模块利用多线程实现ssh并发执行操作
Dec 05 Python
pycharm通过ssh连接远程服务器教程
Feb 12 Python
基于Python的Jenkins的二次开发操作
May 12 Python
基于TensorFlow的CNN实现Mnist手写数字识别
Jun 17 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/10/09 PHP
php正则匹配html中带class的div并选取其中内容的方法
2015/01/13 PHP
借用Google的Javascript API Loader来加速你的网站
2009/01/28 Javascript
Js实现当前点击a标签变色突出显示其他a标签回复原色
2013/11/27 Javascript
jQuery插件Easyui设置datagrid的pageNumber导致两次请求问题的解决方法
2016/08/06 Javascript
原生js实现网易轮播图效果
2020/04/10 Javascript
JS实现发送短信验证后按钮倒计时功能(防止刷新倒计时失效)
2017/07/07 Javascript
简单实现jquery隔行变色
2017/11/09 jQuery
使用vue中的v-for遍历二维数组的方法
2018/03/07 Javascript
JS在if中的强制类型转换方式
2018/07/15 Javascript
react 父子组件之间通讯props
2018/09/08 Javascript
使用RN Animated做一个“添加购物车”动画的方法
2018/09/12 Javascript
javacript replace 正则取字符串中的值并替换【推荐】
2018/09/13 Javascript
微信小程序实现简单跑马灯效果
2020/05/26 Javascript
vue router导航守卫(router.beforeEach())的使用详解
2019/04/19 Javascript
vue源码nextTick使用及原理解析
2019/08/13 Javascript
ant-design-vue 实现表格内部字段验证功能
2019/12/16 Javascript
python的keyword模块用法实例分析
2015/06/30 Python
python实现简单购物商城
2016/05/21 Python
Python 使用os.remove删除文件夹时报错的解决方法
2017/01/13 Python
python win32 简单操作方法
2017/05/25 Python
Python中Numpy ndarray的使用详解
2019/05/24 Python
Django框架HttpResponse对象用法实例分析
2019/11/01 Python
使用Pandas的Series方法绘制图像教程
2019/12/04 Python
python pptx复制指定页的ppt教程
2020/02/14 Python
关于python 跨域处理方式详解
2020/03/28 Python
CSS3 二级导航菜单的制作的示例
2018/04/02 HTML / CSS
探讨HTML5移动开发的几大特性(必看)
2015/12/30 HTML / CSS
《兰亭集序》教学反思
2014/02/11 职场文书
《跨越百年的美丽》教学反思
2014/02/11 职场文书
义和团口号
2014/06/17 职场文书
自我检讨报告
2015/01/28 职场文书
如何利用js在两个html窗口间通信
2021/04/27 Javascript
vue项目多环境配置(.env)的实现
2021/07/21 Vue.js
redis的list数据类型相关命令介绍及使用
2022/01/18 Redis
Python+DeOldify实现老照片上色功能
2022/06/21 Python