028-86922220

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

python中有关文件处理

Python的文件处理

打开文件f = open (“path”,”mode”)

注:在使用以上 mode 打开文件的时候,如果增加了b 模式,表示以二进制方式打开

在榆树等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站建设、做网站 网站设计制作按需定制网站,公司网站建设,企业网站建设,品牌网站设计,成都营销网站建设,成都外贸网站建设公司,榆树网站建设费用合理。

读取文件

注:私有方法在外部访问
在类的内部定义中,所有以双下划线开始的名字都被翻译成前面加单下划线和类名的形式。

class Secretive(object):
    def __inaccessible(self):
        print "Bet you can't see me ..."
    def accessible(self):
        print "The secret message is :"
        self.__inaccessible()
s = Secretive()
print Secretive._Secretive__inaccessible
s._Secretive__inaccessible()

Bet you can't see me ...
#检查继承
isubclass()
检查一个对象是否为一个类的实例
isinstance()

python 中有关文件处理
这里在写一个继承的例子,在子类中重写了构造方法时

#将类都变成新式类,不管是经典还是新式,都算新式类
__metaclass__ =  type
class Bird:
    def __init__(self):
        self.hungry = True
    def eat(self):
        if  self.hungry:
            print "feitian...."
            self.hungry = False
        else:
            print "No.thinks"
class BirdSing(Bird):
    def __init__(self):
        super(BirdSing,self).__init__()
       # Bird.__init__(self)
        self.sound = 'squawk'
    def sing(self):
        print self.sound
s =  BirdSing()
s.sing()
s.eat()
s .eat()

基本的序列和映射规则
序列和映射是对象的集合

class Rectangle(object):
    def lala(self):
        self.width = width
    def __setattr__(self,width, value):
        print "想改,不存在的"
    def __delattr__(self, width):
        print "想删除,也不存在"
    def __getattr__(self,lalala):
        print "你有这个属性吗"
    def __getattribute__(self,name):
        print "想看知道也不存在的"
feitian = Rectangle()
feitian.lala
feitian.width = 1
del feitian.width
feitian.lalala
想看知道也不存在的
想改,不存在的
想删除,也不存在
想看知道也不存在的

Python中的递归生成器

def flatten(nested):
    try:
        try:nested + ''
        except TypeError:pass
        else :
            raise  TypeError
        for sublist in nested:
            for element in  flatten(sublist):
                yield element
    except TypeError:
        yield nested
t =  list(flatten(['1',['bar',['baz']]]))
print  t

文章标题:python中有关文件处理
文章路径:http://www.tsicrk.com/article/joppdj.html

其他资讯

让你的专属顾问为你服务

2.3797s