EOF
là viết tắt của End Of File
. Đây là điểm trong chương trình mà người dùng không thể đọc dữ liệu nữa. Nó có nghĩa là chương trình đọc toàn bộ tệp cho đến khi kết thúc. Ngoài ra, khi đạt đến EOF hoặc kết thúc tệp, các chuỗi trống được trả về dưới dạng đầu ra. Vì vậy, người dùng cần biết liệu tệp có ở EOF hay không
Hướng dẫn này giới thiệu các cách khác nhau để tìm hiểu xem một tệp có ở EOF hay không trong Python
Sử dụng file.read[]
để tìm phần cuối của tệp trong Python
Phương thức file.read[]
là một hàm Python tích hợp được sử dụng để đọc nội dung của một tệp nhất định. Nếu phương thức file.read[]
trả về một chuỗi rỗng làm đầu ra, điều đó có nghĩa là tệp đã đạt đến EOF của nó
Thí dụ
with open["randomfile.txt", "r"] as f:
while True:
file_eof = file_open.read[]
if file_eof == '':
print['End Of File']
break
Lưu ý rằng khi chúng tôi gọi hàm open[]
để mở tệp khi bắt đầu chương trình, chúng tôi sử dụng "r"
làm chế độ chỉ đọc tệp. Cuối cùng, chúng ta sử dụng câu lệnh điều kiện
file_path = 'randomfile.txt'
file_text = open[file_path, "r"]
a = True
while a:
file_line = file_text.readline[]
if not file_line:
print["End Of File"]
a = False
file_text.close[]
0 để kiểm tra đầu ra trả về ở cuối có phải là một chuỗi rỗng khôngSử dụng phương pháp file_path = 'randomfile.txt'
file_text = open[file_path, "r"]
a = True
while a:
file_line = file_text.readline[]
if not file_line:
print["End Of File"]
a = False
file_text.close[]
1 với vòng lặp file_path = 'randomfile.txt'
file_text = open[file_path, "r"]
a = True
while a:
file_line = file_text.readline[]
if not file_line:
print["End Of File"]
a = False
file_text.close[]
2 để tìm phần cuối của tệp trong Python
file_path = 'randomfile.txt'
file_text = open[file_path, "r"]
a = True
while a:
file_line = file_text.readline[]
if not file_line:
print["End Of File"]
a = False
file_text.close[]
file_path = 'randomfile.txt'
file_text = open[file_path, "r"]
a = True
while a:
file_line = file_text.readline[]
if not file_line:
print["End Of File"]
a = False
file_text.close[]
Phương thức
file_path = 'randomfile.txt'
file_text = open[file_path, "r"]
a = True
while a:
file_line = file_text.readline[]
if not file_line:
print["End Of File"]
a = False
file_text.close[]
3 là một hàm Python tích hợp khác để đọc một dòng tệp văn bản hoàn chỉnhVòng lặp
file_path = 'randomfile.txt'
file_text = open[file_path, "r"]
a = True
while a:
file_line = file_text.readline[]
if not file_line:
print["End Of File"]
a = False
file_text.close[]
2 trong Python là một vòng lặp lặp lại điều kiện đã cho trong một khối mã cho đến khi điều kiện đã cho là đúng. Vòng lặp này được sử dụng khi không biết trước số lần lặpSử dụng vòng lặp
file_path = 'randomfile.txt'
file_text = open[file_path, "r"]
a = True
while a:
file_line = file_text.readline[]
if not file_line:
print["End Of File"]
a = False
file_text.close[]
2 với phương thức file_path = 'randomfile.txt'
file_text = open[file_path, "r"]
a = True
while a:
file_line = file_text.readline[]
if not file_line:
print["End Of File"]
a = False
file_text.close[]
1 giúp đọc lặp đi lặp lại các dòng trong tệp văn bản đã choThí dụ
file_path = 'randomfile.txt'
file_text = open[file_path, "r"]
a = True
while a:
file_line = file_text.readline[]
if not file_line:
print["End Of File"]
a = False
file_text.close[]
Vòng lặp
file_path = 'randomfile.txt'
file_text = open[file_path, "r"]
a = True
while a:
file_line = file_text.readline[]
if not file_line:
print["End Of File"]
a = False
file_text.close[]
2 sẽ ngừng lặp lại khi không còn văn bản nào trong tệp văn bản để phương thức file_path = 'randomfile.txt'
file_text = open[file_path, "r"]
a = True
while a:
file_line = file_text.readline[]
if not file_line:
print["End Of File"]
a = False
file_text.close[]
1 đọcSử dụng toán tử hải mã để tìm phần cuối của tệp trong Python
Toán tử Walrus là một toán tử mới trong Python 3. 8. Nó được ký hiệu là
file_path = 'randomfile.txt'
file_text = open[file_path, "r"]
a = True
while a:
file_line = file_text.readline[]
if not file_line:
print["End Of File"]
a = False
file_text.close[]
9. Toán tử này về cơ bản là một toán tử gán được sử dụng để gán các giá trị file = open["randomfile.txt", "r"]
while [f := file.read[]]:
process[f]
file.close[]
0 và sau đó in chúng ngay lập tứcThí dụ
file = open["randomfile.txt", "r"]
while [f := file.read[]]:
process[f]
file.close[]
Ở đây, các giá trị
file = open["randomfile.txt", "r"]
while [f := file.read[]]:
process[f]
file.close[]
0 là các ký tự mà hàm file = open["randomfile.txt", "r"]
while [f := file.read[]]:
process[f]
file.close[]
2 sẽ đọc từ tệp văn bản. Điều đó có nghĩa là nó sẽ ngừng in sau khi hoàn thành tệp