• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 使用Python读写文本文件及编写简单的文本编辑器

使用Python读写文本文件及编写简单的文本编辑器

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python文本编辑器,python文本编辑器下载,python3文本编辑器,python编写文本编辑器,文本编辑器等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

学习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.
</div>

我们要做的就是打开这个文件,然后打印文件内容,我们不在代码中写死文件名称,因为我们如果要读取其他文件的话,就要重新修改代码,解决这个问题的办法就是使用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() 
</div>

上面的代码做了一些有意思的事情,让我们快速的分解一下:

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
</div>
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.

</div>


下面几个文件的命令比较常用:

  • 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() 
</div>

这个程序比较长,所以慢慢来,让它能运行起来。有个办法是,先写几行,运行一下,可以运行再写几行,直到都可以运行。

运行结果
你会看到两个东西,一个是程序的输出:

root@he-desktop:~/mystuff# python ex16.py test.txt
</div>
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.

</div>

还有就是你新建立的文件,打开看看吧。

</div>

您可能想查找下面的文章:

  • Python简单检测文本类型的2种方法【基于文件头及cchardet库】
  • Python简单检测文本类型的2种方法【基于文件头及cchardet库】
  • Python如何实现文本转语音
  • Python读写txt文本文件的操作方法全解析
  • Python的Flask站点中集成xhEditor文本编辑器的教程
  • Python的Flask站点中集成xhEditor文本编辑器的教程
  • Python判断文本中消息重复次数的方法
  • Python判断文本中消息重复次数的方法
  • 使用Python读写文本文件及编写简单的文本编辑器
  • 使用Python读写文本文件及编写简单的文本编辑器

相关文章

  • python获得文件创建时间和修改时间的方法
  • Python单例模式实例分析
  • Python3实现将文件归档到zip文件及从zip文件中读取数据的方法
  • 用Python解析XML的几种常见方法的介绍
  • python实现可以断点续传和并发的ftp程序
  • Python随机数用法实例详解【基于random模块】
  • 使用Python中的cookielib模拟登录网站
  • 在Python的Django框架中生成CSV文件的方法
  • Python fileinput模块使用实例
  • 浅谈python新手中常见的疑惑及解答

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 详解使用Python处理文件目录的相关方法
    • python获取指定目录下所有文件名列表的方法
    • python关键字and和or用法实例
    • Python结巴中文分词工具使用过程中遇到的问题及解决方法
    • pygame学习笔记(5):游戏精灵
    • Python2.x中str与unicode相关问题的解决方法
    • python求pi的方法
    • python实现simhash算法实例
    • 使用python在校内发人人网状态(人人网看状态)
    • Python算法应用实战之栈详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有