1. 程式人生 > >python組合的方式完成授權

python組合的方式完成授權

import time
class FileHandle:
    def __init__(self, name, mode, encoding = 'utf-8'):
        self.file = open(name, mode = mode, encoding = encoding)

    def write(self, line):
        t = time.strftime('%Y-%m-%d %X')
        self.file.write('%s %s' % (t, line))
    def __getattr__(self, item):
        return getattr(self.file, item)

f1 = FileHandle('a.txt', 'r+')
f1.write('111\n')
f1.write('222\n')
f1.seek(0)
print(f1.read())
'''
2019-01-07 12:43:36 111
2019-01-07 12:43:36 222
'''