Cơ bản Thao tác với file txt trong python

Cơ bản Thao tác với file txt trong python

Việc thao tác với file trong python rất quan trong ,bạn cần đọc các thông tin từ file truyền vào trong ứng dụng để run 

Dưới đây là Class đọc và viết file mà mình tự viết các bạn có thể tham khảo 

File Filework.py

import os
import io


class Filework():
    def __init__(self, name: str, noidung='hehe'):
        self.name = name
        self.noidung = noidung
        assert len(name) > 0

    # mode a la append
    def witerfile(self):
        f = io.open(self.name, 'a')
        f.write(self.noidung + '\n')
        f.close()

    # file co utf8
    def witerfileutf8(self):
        f = io.open(self.name, 'a', encoding='utf-8')
        f.write(self.noidung + '\n')
        f.close()

    def readfile(self):
        f = io.open(self.name, mode='r', )
        list_line = f.readlines()
        print(list_line)

    def readfileutf8(self):
        f = io.open(self.name, mode='r', encoding='utf-8')
        list_line = f.readlines()
        for line in list_line:
            print(line)

File main.py

from filework import Filework

if __name__ == '__main__':
    Filework('hien.txt').readfile()