400 028 6601

建站动态

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

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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享文章:python中有关文件处理-创新互联
新闻来源:http://mzwzsj.com/article/deojgh.html

其他资讯

让你的专属顾问为你服务