Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách đọc file text vào list/array Python?


f = open('my_file.txt', 'r+')
my_file_data = f.read()
f.close()

Mở my_file.txt read mode, lưu data vào my_file_data, đóng file. read đọc toàn file. Đọc line by line lưu list:

f = open('my_file', 'r+')
lines = [line for line in f.readlines()]
f.close()