1. 文件读写
python 内置了读写的文件函数,用法和C是兼容
操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开一个文件对象
(又称文件描述符),然后,通过操作系统提供的接口从这个文件对象操作
f = open('/tmp/passwd') print f print f.name # 打印文件名称 print f.mode # 如果没有指定mode时默认为r模式 print f.read(1) # 读取文件内容 r只读 print f.closed # 查看返回值Ture 表示文件关闭 ;False 为开启 f.close() # 关闭文件 print f.closed # open函数模式 r+ 以读写的方式打开,定位文件开头 , 可以写入内容到文件 w 以写的方式打开,打开文件的时候会清空文件的内容,并且不能读 w+ 以读写的方式打开,定位到文件头,并且打开文件的时候也会清空文件的内容 a 以写的方式打开,定位到文件的末尾,是一个追加的操作 , 但并不允许读 a+ 以读写的方式打开,定位到文件的末尾,追加的方式。 在使用以上 mode 打开文件的时候,如果增加了b 模式,表示以二进制方式打开 如: r --->> rb r+ ----->> rb+ w ------>> wb w+ ------>> WB+ a -------->> ra a+ -------->> ra+ 文件的其他操作 f.flush()函数,将缓冲区的内容写入到硬盘中 f.seek(offset[,whence])需要传两个参数,第一个参数offset:偏移量,为正,向右偏移,为负数,向左偏移,为0 ,不偏移 第二个参数 whence:0:指向文件开头; 1:当前位置; 2:文件末尾 f.tell(),返回当前文件指针的偏移量 f.fileno() 函数,返回当前的文件描述符,一个数字 f.isatty() 函数,当前打开的文件是否是一个终端设备 文件与文件的路径 os 模块
import os print os.path.join('hello','nihao')hello/nihao 不管文件是否存在都将结果打印出来
列出tmp 下所有文件
import
print os.listdir('/tmp/')
print os.path.sep 查看系统的路径分隔符号 /
print os.path.abspath('passwd') 查看passwd绝对路径
print os.path.dirname('/tmp/passwd') 查看目录名称
print os.path.basename('/tmp/passwd') 查看文件名称
print os.getcwd() 查看当前工作环境路径
面向对象化(封装,继承,多态)
面向对象的三大特性:封装,继承,多态
# 定义类的过程
class Animal:
pass
# Animal(): 实例化对象的过程;
fentiao = Animal()
# 一个实例:实际<__main__.Animal instance at 0x7fa6b7516cf8>
print fentiao
# 类: 抽象的;__main__.Animal
print Animal
class Animal:
# __init__构造函数,当实例化类时会自动调用;
# self实质上是实例化的对象本身;
def __init__(self, name, age, weight):
# 类的属性
self.name = name
self.age = age
self.weight = weight
print "%s:%s" %(self.name, self)
# 析构函数; 一般用作类的清理工作;
def __del__(self):
print "delete........"
# 实例化对象的过程;
# 自动调用__init__函数;
fentiao = Animal('fentiao', 18, 12)
print fentiao.name
# 自动调用__del__析构函数的内容;
del fentiao
import random
import time
class Animal:
def __init__(self, name, age, weight):
# self.name = name, 把用户传入的参数与对象绑定在一起;
# 类里面的变量称为属性,一般是名词;
self.name = name
self.age = age
self.weight = weight
# 类里面的函数称为方法;一般是动词;
def eat(self):
print "eating......"
time.sleep(random.random())
self.weight += 1
print "%s now weight: %s" %(self.name, self.weight)
def play(self):
print "playing......"
time.sleep(random.random())
self.weight -= 2
print "%s now weight: %s" %(self.name, self.weight)
def __del__(self):
print "delete........"
fentiao = Animal('fentiao', 18, 12)
print fentiao.name
fentiao.eat()
fentiao.play()
del fentiao
![](https://i-blog.csdnimg.cn/blog_migrate/cab2589a8371085677f43bbaea5910d7.png)
继承:
- 父类与子类; 基类与派生类;
- 如果子类没有调用的方法时, 执行父类的;
多态:
如果子类有调用的方法时, 优先执行子类的;
import random
import time
# 新式类
# class 类名(object/父类名称):
# pass
#
# 和经典类:
# class 类名:
# pass
#
class Animal(object):
def __init__(self, name, age, weight):
self.name = name
self.age = age
self.weight = weight
def eat(self):
print "eating......"
def play(self):
print "playing......"
# Cat: 子类,/派生类; Animal:父类/基类;
class Cat(Animal):
# 当子类没有构造函数时, 自动去调用父类的;
def __init__(self, name, age, weight, country):
# 继承的方法2:建议使用, 可维护性比较高;
super(Cat, self).__init__(name, age, weight)
self.country = country
# 继承的方法1:但不建议使用, 可维护性比较低;
# Animal.__init__(self,name, age, weight)
# self.name = name
# self.age = age
# self.weight = weight
# self.country = country
def eat(self):
super(Cat, self).eat()
print self.name
# 子类不存在__init__函数, 调用父类的;
# 子类存在__init__函数, 调用子类的;
fentiao = Cat('fentiao', 12, 18, 'china')
print fentiao.country
fentiao.eat()
![](https://i-blog.csdnimg.cn/blog_migrate/6ce7cb43eb6defebe38499c907f2740f.png)
# 新式类多继承时, 继承算法是广度优先; 查看方式A.mro()
# A - (B,C) -D
# 经典类多继承时, 继承算法是深度优先;
class D(object):
def test(self):
print "D test"
class C(D):
def test(self):
print "C test"
class B(D):
def test(self):
print "B test"
class A(B,C):
def test(self):
print "A test"
a = A()
a.test()
A test 找A如果A中没有东西会去找B如果B也没有就找C最后要是都没有的话会找object文件返回值
多态: 当父类和子类有相同方法时,优先执行子类方法,
类的属性:
class Animal(object):
"""
Animals class
"""
pass
print Animal.mro()
print Animal.__doc__
print Animal.__name__
fentiao = Animal()
print fentiao.__doc__
# 打印对象的类名
print fentiao.__class__
print fentiao.__module__
![](https://i-blog.csdnimg.cn/blog_migrate/059adcb8309c37b42b34768b4eca683f.png)
两个特别的函数(魔术函数)
__repr__(self):调用时自动调用
__str__(self):打印对象时候自动调用
class Animal(object): def __init__(self,name): self.name = name def __repr__(self): """ 调试时自动调用, a = Animal("xxx") a :return: """ return "Animals:%s" %(self.name) def __str__(self): """打印对象时,自动调用""" return "%s" %(self.name) a = Animal('fentiao') print a print a.__repr__() print a.__str__()