Hướng dẫn python read line from file as string - python đọc dòng từ tệp dưới dạng chuỗi

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:

Show
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.

  1. Nó mở tệp để đọc và bắt đầu một vài biến được sử dụng.
  2. 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
  3. 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.
  4. Đó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
    7lineList9 with0with1
  • #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
    Using for loop
    Line1: Geeks
    Line2: for
    Line3: Geeks
    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()
    
    1
    Line1: Geeks
    Line2: for
    Line3: Geeks
    71
  • #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

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
3
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
4
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
5
Line1 Geeks
Line2 for
Line3 Geeks
01

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: Geeks
7

Line1: Geeks
Line2: for
Line3: Geeks
8

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: Geeks
0
#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: Geeks
2

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
Line1 Geeks
Line2 for
Line3 Geeks
7
#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: Geeks
29

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
3
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
4
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
5
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
6

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
['Geeks\n', 'For\n', 'Geeks']
['Geeks', 'For', 'Geeks']
3
Line1: Geeks
Line2: for
Line3: Geeks
2
['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: Geeks0Using 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: Geeks
7lineList9 with0with1

Hướng dẫn python read line from file as string - python đọc dòng từ tệp dưới dạng chuỗi

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;

f.readlines()6

Line1: Geeks
Line2: for
Line3: Geeks
8

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: Geeks
0
#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: Geeks
2

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
Line1 Geeks
Line2 for
Line3 Geeks
7
#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: Geeks
29

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
3
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
4
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
5
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
6

#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
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
0
Using 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

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7lineList9 with0with1

#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()
05
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
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()
1
Line1: Geeks
Line2: for
Line3: Geeks
71

Line1: Geeks
Line2: for
Line3: 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() 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

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
3
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
4
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
5
Line1 Geeks
Line2 for
Line3 Geeks
01

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: Geeks
7

Line1: Geeks
Line2: for
Line3: Geeks
8

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: Geeks
0
#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: Geeks
2

['Geeks\n', 'For\n', 'Geeks']
['Geeks', 'For', 'Geeks']
3
Line1: Geeks
Line2: for
Line3: Geeks
222147
Line1: Geeks
Line2: for
Line3: Geeks
6

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
3
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
4
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
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()
52

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
0
Using 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

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
['Geeks\n', 'For\n', 'Geeks']
['Geeks', 'For', 'Geeks']
3
Line1: Geeks
Line2: for
Line3: Geeks
2
['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: Geeks
8

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()
66
Line1: Geeks
Line2: for
Line3: Geeks
1
Line1: Geeks
Line2: for
Line3: Geeks
2
Line1: Geeks
Line2: for
Line3: 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()
70

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
#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()
74
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
3
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
4
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
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()
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()
66
Line1: Geeks
Line2: for
Line3: Geeks
1
Line1: Geeks
Line2: for
Line3: Geeks
2
Line1: Geeks
Line2: for
Line3: 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()
70

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
#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()
89
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
3
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
4
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
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()
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

Output:

['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()
8

Các

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
Line1: Geeks
Line2: for
Line3: Geeks
13

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
0
#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: Geeks
2

['Geeks\n', 'For\n', 'Geeks']
['Geeks', 'For', 'Geeks']
3
Line1: Geeks
Line2: for
Line3: Geeks
2
Line1: Geeks
Line2: for
Line3: Geeks
19
Line1: Geeks
Line2: for
Line3: Geeks
6

#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()
66
Line1: Geeks
Line2: for
Line3: Geeks
1
Line1: Geeks
Line2: for
Line3: Geeks
2
Line1: Geeks
Line2: for
Line3: Geeks
08
Line1: Geeks
Line2: for
Line3: Geeks
11

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
Line1 Geeks
Line2 for
Line3 Geeks
7
#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: Geeks
29

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
3
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
4
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
5
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
6

#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
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
0
Using 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()
05
['Geeks\n', 'For\n', 'Geeks']
['Geeks', 'For', 'Geeks']
3
Line1: Geeks
Line2: for
Line3: Geeks
2
['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: Geeks
0
#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: Geeks
2

['Geeks\n', 'For\n', 'Geeks']
['Geeks', 'For', 'Geeks']
3
Line1: Geeks
Line2: for
Line3: Geeks
2
Line1: Geeks
Line2: for
Line3: Geeks
52
Line1: Geeks
Line2: for
Line3: Geeks
6

#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()
66
Line1: Geeks
Line2: for
Line3: Geeks
1
Line1: Geeks
Line2: for
Line3: Geeks
2
Line1: Geeks
Line2: for
Line3: Geeks
08
Line1: Geeks
Line2: for
Line3: Geeks
11

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7lineList9 with0with1

#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
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
0
Using 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

Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
7lineList9 with0with1

#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
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
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()
1
Line1: Geeks
Line2: for
Line3: Geeks
71

Line1: Geeks
Line2: for
Line3: Geeks
76
#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: Geeks
2
['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: Geeks
0
#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: Geeks
2

['Geeks\n', 'For\n', 'Geeks']
['Geeks', 'For', 'Geeks']
3
Line1: Geeks
Line2: for
Line3: Geeks
2
Line1: Geeks
Line2: for
Line3: Geeks
90
Line1: Geeks
Line2: for
Line3: Geeks
6

#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()
66
Line1: Geeks
Line2: for
Line3: Geeks
1
Line1: Geeks
Line2: for
Line3: Geeks
2
Line1: Geeks
Line2: for
Line3: Geeks
08
Line1: Geeks
Line2: for
Line3: Geeks
11

#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()
05
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
0
Using 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()
05
['Geeks\n', 'For\n', 'Geeks']
['Geeks', 'For', 'Geeks']
3
Line1: Geeks
Line2: for
Line3: Geeks
2
['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