使用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基础教程之基本内置数据类型介绍
Feb 20 Python
python使用marshal模块序列化实例
Sep 25 Python
python基于pyDes库实现des加密的方法
Apr 29 Python
Python探索之创建二叉树
Oct 25 Python
python pandas修改列属性的方法详解
Jun 09 Python
Python实现DDos攻击实例详解
Feb 02 Python
Python 使用指定的网卡发送HTTP请求的实例
Aug 21 Python
Python笔记之观察者模式
Nov 20 Python
python selenium实现发送带附件的邮件代码实例
Dec 10 Python
Python+OpenCV图像处理——图像二值化的实现
Oct 24 Python
python 对一幅灰度图像进行直方图均衡化
Oct 27 Python
浅谈tf.train.Saver()与tf.train.import_meta_graph的要点
May 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
Memcached常用命令以及使用说明详解
2013/06/27 PHP
PHP记录页面停留时间的方法
2016/03/30 PHP
js动画效果制件让图片组成动画代码分享
2014/01/14 Javascript
jquery左右全屏大尺寸多图滑动效果代码分享
2015/08/28 Javascript
JavaScritp添加url参数并将参数加入到url中及更改url参数的方法
2015/10/26 Javascript
举例说明JavaScript中的实例对象与原型对象
2016/03/11 Javascript
jQuery+ajax实现滚动到页面底部自动加载图文列表效果(类似图片懒加载)
2016/06/07 Javascript
jQuery表单验证插件解析(推荐)
2016/07/21 Javascript
JavaScript编码风格指南(中文版)
2016/08/26 Javascript
微信小程序 数组中的push与concat的区别
2017/01/05 Javascript
nodejs基础知识
2017/02/03 NodeJs
VUE实现日历组件功能
2017/03/13 Javascript
认识less和webstrom的less配置方法
2017/08/02 Javascript
JS写滑稽笑脸运动效果
2020/05/28 Javascript
Python 开发Activex组件方法
2009/11/08 Python
跟老齐学Python之集合(set)
2014/09/24 Python
Python判断列表是否已排序的各种方法及其性能分析
2016/06/20 Python
打包发布Python模块的方法详解
2016/09/18 Python
python中WSGI是什么,Python应用WSGI详解
2017/11/24 Python
基于DATAFRAME中元素的读取与修改方法
2018/06/08 Python
详解Python odoo中嵌入html简单的分页功能
2019/05/29 Python
Python之变量类型和if判断方式
2020/05/05 Python
Java爬虫技术框架之Heritrix框架详解
2020/07/22 Python
详解Python的爬虫框架 Scrapy
2020/08/03 Python
在Java开发中如何选择使用哪种集合类
2016/08/09 面试题
成人教育自我鉴定
2013/11/01 职场文书
会议接待欢迎词
2014/01/12 职场文书
明星员工获奖感言
2014/08/14 职场文书
公务员培的训心得体会
2014/09/01 职场文书
计划生育个人总结
2015/03/02 职场文书
2015年公民道德宣传日活动总结
2015/03/23 职场文书
《西游记》读后感(3篇)
2019/09/20 职场文书
MySQL into_Mysql中replace与replace into用法案例详解
2021/09/14 MySQL
vue cli4中mockjs在dev环境和build环境的配置详情
2022/04/06 Vue.js
vue代码分块和懒加载非必要资源文件
2022/04/11 Vue.js
Nginx代理Redis哨兵主从配置的实现
2022/07/15 Servers