Skip to main content

Python 文件的读取

Published 2 min readMarkdown
Python 文件的读取

文件的读取

文件操作的模式之读取

模式介绍
r读取文件
rb二进制形式读取文件

文件对象的操作模式之读

方法名参数介绍举例
read(seed)无返回整个文件字符串f.read()
readlines无返回文件列表f.readlines()
readline无返回文件中的一行f.readline()
mode无文件模式f.mode
name无返回文件名称f.name
closed无文件是否关闭f.closed
  • 操作完成后,必须使用close方法!!

  • with函数

代码

# coding:utf-8

import os


def create_package(path):
    if os.path.exists(path):
        raise Exception('%s 已经存在不可创建' % path)
    os.makedirs(path)
    init_path = os.path.join(path, '__init__.py')
    f = open(init_path, 'w')
    f.write("# coding:utf-8\n")
    f.close()


class Open(object):
    def __init__(self, path, mode='w', is_return=True):
        self.path = path
        self.mode = mode
        self.is_return = is_return

    def write(self, message):
        f = open(self.path, mode=self.mode)
        if self.is_return:
            message = '%s\n' % message
        f.write(message)
        f.close()

    def read(self, is_strip=True):
        result = []
        with open(self.path, mode=self.mode) as f:
            _data = f.readlines()
        for line in _data:
            if is_strip:
                temp = line.strip()
                if temp != '':
                    result.append(temp)
            else:
                if line != '':
                    result.append(line)
        return result


if __name__ == '__main__':
    current_path = os.getcwd()
    # path = os.path.join(current_path, 'test1')
    # create_package(path)
    # open_path = os.path.join(current_path, 'b.txt')
    o = Open('package_datetime.py', mode='r')
    # o.write('你好 小慕')
    # data = o.read(is_strip=False)
    data = o.read()
    print(data)

1 view

No comments yet. Be the first to comment.

More from this blog