使用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 3.x 连接数据库示例(pymysql 方式)
Jan 19 Python
Python实现识别图片内容的方法分析
Jul 11 Python
对numpy中二进制格式的数据存储与读取方法详解
Nov 01 Python
Appium Python自动化测试之环境搭建的步骤
Jan 23 Python
20行python代码实现人脸识别
May 05 Python
PYTHON EVAL的用法及注意事项解析
Sep 06 Python
python使用opencv实现马赛克效果示例
Sep 28 Python
深入浅析python的第三方库pandas
Feb 13 Python
基于python实现数组格式参数加密计算
Apr 21 Python
解决python中0x80072ee2错误的方法
Jul 19 Python
Python下载网易云歌单歌曲的示例代码
Aug 12 Python
Django程序的优化技巧
Apr 29 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
libmysql.dll与php.ini是否真的要拷贝到c:\windows目录下呢
2010/03/15 PHP
php foreach正序倒序输出示例代码
2014/07/01 PHP
PHP静态文件生成类实例
2014/11/29 PHP
php实现将字符串按照指定距离进行分割的方法
2015/03/14 PHP
php更新cookie内容的详细方法
2019/09/30 PHP
超级退弹代码
2008/07/07 Javascript
javascript getElementsByName()的用法说明
2009/07/31 Javascript
JavaScript 面向对象编程(2) 定义类
2010/05/18 Javascript
谷歌浏览器调试JavaScript小技巧
2014/12/29 Javascript
jQuery实现信息提示框(带有圆角框与动画)效果
2015/08/07 Javascript
详解JavaScript中js对象与JSON格式字符串的相互转换
2017/02/14 Javascript
js实现手机发送验证码功能
2017/03/13 Javascript
css和js实现弹出登录居中界面完整代码
2017/11/26 Javascript
vue axios 给生产环境和发布环境配置不同的接口地址(推荐)
2018/05/08 Javascript
JS中创建自定义类型的常用模式总结【工厂模式,构造函数模式,原型模式,动态原型模式等】
2019/01/19 Javascript
一篇文章,教你学会Vue CLI 插件开发
2019/04/17 Javascript
浅谈vuex中store的命名空间
2019/11/08 Javascript
解决vue 使用setTimeout,离开当前路由setTimeout未销毁的问题
2020/07/21 Javascript
[02:28]DOTA2 2017国际邀请赛小组赛回顾
2017/08/09 DOTA
跟老齐学Python之不要红头文件(2)
2014/09/28 Python
基于Python pip用国内镜像下载的方法
2018/06/12 Python
​如何愉快地迁移到 Python 3
2019/04/28 Python
python Pandas库基础分析之时间序列的处理详解
2019/07/13 Python
详解python polyscope库的安装和例程
2020/11/13 Python
斯洛伐克家具和时尚装饰品购物网站:Butlers.sk
2019/09/08 全球购物
俄罗斯汽车零件和配件在线商店:CarvilleShop
2019/11/29 全球购物
最新会计专业求职信范文
2014/01/28 职场文书
超市优秀员工事迹材料
2014/05/01 职场文书
见习报告格式要求
2014/11/04 职场文书
2015应届毕业生自荐信范文
2015/03/05 职场文书
毕业生学校组织意见
2015/06/04 职场文书
2016企业先进集体事迹材料
2016/02/25 职场文书
初一语文教学反思
2016/03/03 职场文书
2019 入党申请书范文
2019/07/10 职场文书
vue实现可拖拽的dialog弹框
2021/05/13 Vue.js
Win11运行cmd提示“请求的操作需要提升”的两种解决方法
2022/07/07 数码科技