python 中有关文件处理

发布时间:2020-06-30 03:42:09 作者:飞天喜欢yu
来源:网络 阅读:609

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()
<unbound method 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
推荐阅读:
  1. Python中的文件处理操作
  2. python bin文件处理

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python 文件处理 中有

上一篇:base64编码

下一篇:ones刻录软件说明

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》