python学习笔记:二
2013-11-26 20:58
423 查看
文件操作:
Open函数格式
handle = open(file_name, ‘r’) #open后接文件名和打开方式,‘r’:只读,‘w’:可写,‘+’:读写, 'a' 表示添加,'b'表示二进制访问。
Example:
类的定义:
PEP:
在本书中你会经常看到 PEP 这个字眼。 一个 PEP 就是一个 Python 增强提案(Python Enhancement Proposal), 这也是在新版 Python 中增加新特性的方式。 从初学者的角度看,它们是一些高级读物, 它们不但提供了新特性的完整描述, 还有添加这些新特性的理由, 如果需要的话, 还会提供新的语法、 技术实现细节、向后兼容信息等等。在一个新特性被整合进 Python 之前,必须通过 Python 开发社区,PEP 作者及实现者,还有 Python 的创始人,Guid ovan Rossum(Python 终身的仁慈的独裁者)的一致同意。PEP1 阐述了 PEP 的目标及书写指南。在 PEP0 中可以找到所有的 PEP。 PEP 索引的网址是: http://python.org/dev/peps。
Open函数格式
handle = open(file_name, ‘r’) #open后接文件名和打开方式,‘r’:只读,‘w’:可写,‘+’:读写, 'a' 表示添加,'b'表示二进制访问。
Example:
__author__ = 'Joe' #! coding: utf-8 def write_into_file(): """向文件test.txt文件中,写入信息""" try: f_obj = open("test.txt", "w") f_str = 'a' while "q" != f_str: f_str = raw_input("输入文件内容(以q结束):") f_obj.write(f_str) f_obj.write('\n') f_obj.close() return True except IOError, e: print "异常:", e return False def read_from_file(): """从文件test.txt读取信息""" try: f_obj = open("test.txt", "r") print "test.txt 的内容:" for eachLine in f_obj: print eachLine, f_obj.close() return True except IOError, e: print '异常:', e return False def main(): write_success = write_into_file() if write_success: read_from_file() else: print "写入文件失败" #调用主函数 main()
类的定义:
__author__ = 'Joe' class FooClass(object): """my very first class: FooClass""" version = 0.1 # class (data) attribute def __init__(self, nm='John Doe'): """constructor""" self.name = nm # class instance (data) attribute print 'Created a class instance for', nm def show_name(self): """display instance attribute and class name""" print 'Your name is', self.name print 'My name is', self.__class__.__name__ def show_ver(self): """display class(static) attribute""" print self.version # references FooClass.version def addme2me (self, x): # does not use 'self' """apply + operation to argument""" return x + x my_obj = FooClass() my_obj.show_name() my_obj.show_ver()
PEP:
在本书中你会经常看到 PEP 这个字眼。 一个 PEP 就是一个 Python 增强提案(Python Enhancement Proposal), 这也是在新版 Python 中增加新特性的方式。 从初学者的角度看,它们是一些高级读物, 它们不但提供了新特性的完整描述, 还有添加这些新特性的理由, 如果需要的话, 还会提供新的语法、 技术实现细节、向后兼容信息等等。在一个新特性被整合进 Python 之前,必须通过 Python 开发社区,PEP 作者及实现者,还有 Python 的创始人,Guid ovan Rossum(Python 终身的仁慈的独裁者)的一致同意。PEP1 阐述了 PEP 的目标及书写指南。在 PEP0 中可以找到所有的 PEP。 PEP 索引的网址是: http://python.org/dev/peps。
相关文章推荐
- python学习笔记(1)
- 【Python学习笔记】5. 函数
- Python学习笔记:作用域和命名空间
- python3.4学习笔记(五) IDLE显示行号问题,插件安装和其他开发工具介绍
- Python学习笔记(零)-- Python简介
- python学习笔记
- python MySQLdb学习笔记
- python学习笔记(三)
- Python学习笔记 - 函数
- python 系统学习笔记(八)---文件操作
- Python3学习笔记:迭代器与生成器
- 8. Python脚本学习笔记八个别细节一
- 廖雪峰Python教程 学习笔记11 面向对象高级编程
- Python学习笔记
- Python学习笔记之一: 函数参数
- 【python学习笔记】10:Beautiful Soup模块的使用
- python学习笔记
- python学习笔记(一)
- python学习笔记【MIT6 二分法,牛顿,拉复生方法,列表】
- Python学习小笔记2