Có vẻ như, nói chung, những gì bạn muốn là f.readlines[]
. Cụ thể, hãy xem tài liệu Python này về cách thực hiện. Mã của bạn để mở và hiển thị một dòng tệp từng dòng sẽ trông giống như thế này, tuy nhiên:
with open["file.txt", "r"] as f:
for line in f:
print[line]
Ngoài ra, bạn có thể viết vào danh sách và sau đó gọi nó sau:
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
Mã thứ hai này làm gì, như sau.
- Nó mở tệp để đọc và bắt đầu một vài biến được sử dụng.
- Nó chạy qua từng dòng tài liệu của bạn và thêm nó vào danh sách
lineList
- Sau khi nó chạy qua mọi dòng trong tài liệu của bạn, nó hiển thị tổng số dòng và yêu cầu bạn chọn một dòng để hiển thị. Sau đó, mã kết thúc.
- Đóng tay cầm tập tin. Nếu bạn đang sử dụng Trình quản lý ngữ cảnh [
with
], điều này sẽ được thực hiện tự động.
Prerequisites:
Using for loop Line1: Geeks Line2: for Line3: Geeks
7lineList
9with
0with
1
05#STEP 1 f = open["file.txt","r"] #STEP 2 linelist = f.readlines[] count = len[linelist] #STEP 3 print[count] input = input["display line number:"] print[lineList[input]] # STEP 4 f.close[]
Using for loop Line1: Geeks Line2: for Line3: Geeks
4
1#STEP 1 f = open["file.txt","r"] #STEP 2 linelist = f.readlines[] count = len[linelist] #STEP 3 print[count] input = input["display line number:"] print[lineList[input]] # STEP 4 f.close[]
Line1: Geeks Line2: for Line3: Geeks
71
05#STEP 1 f = open["file.txt","r"] #STEP 2 linelist = f.readlines[] count = len[linelist] #STEP 3 print[count] input = input["display line number:"] print[lineList[input]] # STEP 4 f.close[]
02#STEP 1 f = open["file.txt","r"] #STEP 2 linelist = f.readlines[] count = len[linelist] #STEP 3 print[count] input = input["display line number:"] print[lineList[input]] # STEP 4 f.close[]
03#STEP 1 f = open["file.txt","r"] #STEP 2 linelist = f.readlines[] count = len[linelist] #STEP 3 print[count] input = input["display line number:"] print[lineList[input]] # STEP 4 f.close[]
04#STEP 1 f = open["file.txt","r"] #STEP 2 linelist = f.readlines[] count = len[linelist] #STEP 3 print[count] input = input["display line number:"] print[lineList[input]] # STEP 4 f.close[]
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Using for loop Line1: Geeks Line2: for Line3: Geeks3
Using for loop Line1: Geeks Line2: for Line3: Geeks4
Using for loop Line1: Geeks Line2: for Line3: Geeks5
Line1 Geeks Line2 for Line3 Geeks01
Chế độ truy cập & nbsp;
Mở tệp & nbsp;
Example:
Python3
____10
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 #STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
2#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
3#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
4#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
5#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
4#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
3#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
8Đóng một tập tin & nbsp;
Line1: Geeks Line2: for Line3: Geeks7
Line1: Geeks Line2: for Line3: Geeks8
Python cung cấp các chức năng sẵn có để tạo, viết và đọc các tệp. Có hai loại tệp có thể được xử lý trong Python, tệp văn bản thông thường và tệp nhị phân [được viết bằng ngôn ngữ nhị phân, 0S và 1S]. Trong bài viết này, chúng tôi sẽ nghiên cứu đọc từng dòng từ một tệp.
Phương pháp 1: Đọc từng dòng tệp bằng cách sử dụng readlines []
Using for loop Line1: Geeks Line2: for Line3: Geeks0
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 Using for loop Line1: Geeks Line2: for Line3: Geeks2
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Line1 Geeks Line2 for Line3 Geeks7
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 Line1: Geeks Line2: for Line3: Geeks29
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Using for loop Line1: Geeks Line2: for Line3: Geeks3
Using for loop Line1: Geeks Line2: for Line3: Geeks4
Using for loop Line1: Geeks Line2: for Line3: Geeks5
Using for loop Line1: Geeks Line2: for Line3: Geeks6
Using for loop Line1: Geeks Line2: for Line3: Geeks7
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']3
Line1: Geeks Line2: for Line3: Geeks2
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']5
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']6
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']7
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']8
Output:
Line1: Geeks Line2: for Line3: Geeks
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
05Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
0Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
9#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 ['Geeks\n', 'For\n', 'Geeks']
['Geeks', 'For', 'Geeks']
1
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
Using for loop Line1: Geeks Line2: for Line3: Geeks7
lineList
9 with
0with
1Example:
Python3
____10
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 #STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
2#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
3#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
4#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
5#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
4#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
3#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
8Đóng một tập tin & nbsp;
f.readlines[]
6
Line1: Geeks Line2: for Line3: Geeks8
Python cung cấp các chức năng sẵn có để tạo, viết và đọc các tệp. Có hai loại tệp có thể được xử lý trong Python, tệp văn bản thông thường và tệp nhị phân [được viết bằng ngôn ngữ nhị phân, 0S và 1S]. Trong bài viết này, chúng tôi sẽ nghiên cứu đọc từng dòng từ một tệp.
Using for loop Line1: Geeks Line2: for Line3: Geeks0
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 Using for loop Line1: Geeks Line2: for Line3: Geeks2
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Line1 Geeks Line2 for Line3 Geeks7
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 Line1: Geeks Line2: for Line3: Geeks29
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Using for loop Line1: Geeks Line2: for Line3: Geeks3
Using for loop Line1: Geeks Line2: for Line3: Geeks4
Using for loop Line1: Geeks Line2: for Line3: Geeks5
Using for loop Line1: Geeks Line2: for Line3: Geeks6
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
05Using for loop Line1: Geeks Line2: for Line3: Geeks0
Using for loop Line1: Geeks Line2: for Line3: Geeks9
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 ['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']1
Using for loop Line1: Geeks Line2: for Line3: Geeks7
lineList
9 with
0with
1#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
05#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
06#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
05Using for loop Line1: Geeks Line2: for Line3: Geeks4
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 Line1: Geeks Line2: for Line3: Geeks71
Line1: Geeks Line2: for Line3: Geeks8
Output:
Line1 Geeks Line2 for Line3 Geeks
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
05#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
02 #STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
03 #STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
04
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Using for loop Line1: Geeks Line2: for Line3: Geeks3
Using for loop Line1: Geeks Line2: for Line3: Geeks4
Using for loop Line1: Geeks Line2: for Line3: Geeks5
Line1 Geeks Line2 for Line3 Geeks01
Example:
Python3
____10
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 #STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
2#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
3#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
4#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
5#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
4#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
3#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
8Đóng một tập tin & nbsp;
Line1: Geeks Line2: for Line3: Geeks7
Line1: Geeks Line2: for Line3: Geeks8
Python cung cấp các chức năng sẵn có để tạo, viết và đọc các tệp. Có hai loại tệp có thể được xử lý trong Python, tệp văn bản thông thường và tệp nhị phân [được viết bằng ngôn ngữ nhị phân, 0S và 1S]. Trong bài viết này, chúng tôi sẽ nghiên cứu đọc từng dòng từ một tệp.
Using for loop Line1: Geeks Line2: for Line3: Geeks0
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 Using for loop Line1: Geeks Line2: for Line3: Geeks2
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']3
Line1: Geeks Line2: for Line3: Geeks222147
Line1: Geeks Line2: for Line3: Geeks6
Using for loop Line1: Geeks Line2: for Line3: Geeks3
Using for loop Line1: Geeks Line2: for Line3: Geeks4
Using for loop Line1: Geeks Line2: for Line3: Geeks5
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
52Using for loop Line1: Geeks Line2: for Line3: Geeks7
Using for loop Line1: Geeks Line2: for Line3: Geeks0
Using for loop Line1: Geeks Line2: for Line3: Geeks9
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 ['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']1
Using for loop Line1: Geeks Line2: for Line3: Geeks7
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']3
Line1: Geeks Line2: for Line3: Geeks2
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']5
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']6
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']7
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']8
Line1: Geeks Line2: for Line3: Geeks8
Output:
Using for loop Line1: Geeks Line2: for Line3: Geeks
Phương pháp 4: Đọc một dòng theo từng dòng bằng cách sử dụng cho vòng lặp và danh sách hiểu biết
Danh sách hiểu bao gồm các dấu ngoặc chứa biểu thức, được thực thi cho từng phần tử cùng với vòng lặp để lặp lại trên mỗi phần tử. Chúng tôi đã xóa tất cả các ký tự dòng mới khỏi danh sách.
Thí dụ
Python3
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
66Line1: Geeks Line2: for Line3: Geeks1
Line1: Geeks Line2: for Line3: Geeks2
Line1: Geeks Line2: for Line3: Geeks3
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
70Using for loop Line1: Geeks Line2: for Line3: Geeks7
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
72#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 #STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
74Using for loop Line1: Geeks Line2: for Line3: Geeks3
Using for loop Line1: Geeks Line2: for Line3: Geeks4
Using for loop Line1: Geeks Line2: for Line3: Geeks5
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
78['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']3
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
80#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
66Line1: Geeks Line2: for Line3: Geeks1
Line1: Geeks Line2: for Line3: Geeks2
Line1: Geeks Line2: for Line3: Geeks3
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
70Using for loop Line1: Geeks Line2: for Line3: Geeks7
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
72#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 #STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
89Using for loop Line1: Geeks Line2: for Line3: Geeks3
Using for loop Line1: Geeks Line2: for Line3: Geeks4
Using for loop Line1: Geeks Line2: for Line3: Geeks5
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
78['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']3
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
80Output:
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']
Với tuyên bố
Trong các cách tiếp cận trên, mỗi khi tập tin được mở, cần phải đóng một cách rõ ràng. Nếu người ta quên đóng tệp, nó có thể giới thiệu một số lỗi trong mã, tức là nhiều thay đổi trong các tệp không có hiệu lực cho đến khi tệp được đóng đúng. Để ngăn chặn điều này với tuyên bố có thể được sử dụng. Tuyên bố với Python được sử dụng trong xử lý ngoại lệ để làm cho mã sạch hơn và dễ đọc hơn nhiều. Nó đơn giản hóa việc quản lý các tài nguyên chung như luồng tệp. Quan sát ví dụ mã sau về cách sử dụng với câu lệnh làm cho mã sạch hơn. Không cần gọi tệp.close [] khi sử dụng với câu lệnh. Với tuyên bố tự đảm bảo mua lại và phát hành các tài nguyên thích hợp.
Example:
Python3
____10
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 #STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
2#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
3#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
4#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
5#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
4#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
3#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
8Các
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Line1: Geeks Line2: for Line3: Geeks13
Using for loop Line1: Geeks Line2: for Line3: Geeks0
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 Using for loop Line1: Geeks Line2: for Line3: Geeks2
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']3
Line1: Geeks Line2: for Line3: Geeks2
Line1: Geeks Line2: for Line3: Geeks19
Line1: Geeks Line2: for Line3: Geeks6
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
66Line1: Geeks Line2: for Line3: Geeks1
Line1: Geeks Line2: for Line3: Geeks2
Line1: Geeks Line2: for Line3: Geeks08
Line1: Geeks Line2: for Line3: Geeks11
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Line1 Geeks Line2 for Line3 Geeks7
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 Line1: Geeks Line2: for Line3: Geeks29
Using for loop Line1: Geeks Line2: for Line3: Geeks7
Using for loop Line1: Geeks Line2: for Line3: Geeks3
Using for loop Line1: Geeks Line2: for Line3: Geeks4
Using for loop Line1: Geeks Line2: for Line3: Geeks5
Using for loop Line1: Geeks Line2: for Line3: Geeks6
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
05Using for loop Line1: Geeks Line2: for Line3: Geeks0
Using for loop Line1: Geeks Line2: for Line3: Geeks9
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 ['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']1
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
05['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']3
Line1: Geeks Line2: for Line3: Geeks2
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']5
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']6
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']7
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']8
Using for loop Line1: Geeks Line2: for Line3: Geeks0
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 Using for loop Line1: Geeks Line2: for Line3: Geeks2
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']3
Line1: Geeks Line2: for Line3: Geeks2
Line1: Geeks Line2: for Line3: Geeks52
Line1: Geeks Line2: for Line3: Geeks6
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
66Line1: Geeks Line2: for Line3: Geeks1
Line1: Geeks Line2: for Line3: Geeks2
Line1: Geeks Line2: for Line3: Geeks08
Line1: Geeks Line2: for Line3: Geeks11
Using for loop Line1: Geeks Line2: for Line3: Geeks7
lineList
9 with
0with
1#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
05Using for loop Line1: Geeks Line2: for Line3: Geeks0
Using for loop Line1: Geeks Line2: for Line3: Geeks9
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 ['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']1
Using for loop Line1: Geeks Line2: for Line3: Geeks7
lineList
9 with
0with
1#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
05Using for loop Line1: Geeks Line2: for Line3: Geeks4
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 Line1: Geeks Line2: for Line3: Geeks71
Line1: Geeks Line2: for Line3: Geeks76
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
06#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
05['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']3
Line1: Geeks Line2: for Line3: Geeks2
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']5
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']6
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']7
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']8
Using for loop Line1: Geeks Line2: for Line3: Geeks0
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 Using for loop Line1: Geeks Line2: for Line3: Geeks2
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']3
Line1: Geeks Line2: for Line3: Geeks2
Line1: Geeks Line2: for Line3: Geeks90
Line1: Geeks Line2: for Line3: Geeks6
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
66Line1: Geeks Line2: for Line3: Geeks1
Line1: Geeks Line2: for Line3: Geeks2
Line1: Geeks Line2: for Line3: Geeks08
Line1: Geeks Line2: for Line3: Geeks11
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
05#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
02 #STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
03 #STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
04#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
05Using for loop Line1: Geeks Line2: for Line3: Geeks0
Using for loop Line1: Geeks Line2: for Line3: Geeks9
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
1 ['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']1
#STEP 1
f = open["file.txt","r"]
#STEP 2
linelist = f.readlines[]
count = len[linelist]
#STEP 3
print[count]
input = input["display line number:"]
print[lineList[input]]
# STEP 4
f.close[]
05['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']3
Line1: Geeks Line2: for Line3: Geeks2
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']5
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']6
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']7
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']8
Output:
Using readlines[] Line1: Geeks Line2: for Line3: Geeks Using readline[] Line1: Geeks Line2: for Line3: Geeks Using for loop Line1: Geeks Line2: for Line3: Geeks