Hướng dẫn how do you read and append a file in python? - làm thế nào để bạn đọc và nối một tệp trong python?

Bạn đang tìm kiếm chế độ ________ 6/________ 7/________ 8, cho phép cả các hoạt động đọc và ghi vào các tệp.

Với r+, vị trí ban đầu lúc đầu, nhưng đọc nó một lần sẽ đẩy nó về cuối, cho phép bạn nối thêm. Với a+, vị trí ban đầu ở cuối.

with open("filename", "r+") as f:
    # here, position is initially at the beginning
    text = f.read()
    # after reading, the position is pushed toward the end

    f.write("stuff to append")
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")

Nếu bạn cần thực hiện toàn bộ đọc lại, bạn có thể quay lại vị trí bắt đầu bằng cách thực hiện

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
1.

with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")

(Đọc thêm: Sự khác biệt giữa 'R+' và 'A+' khi mở tệp trong Python là gì?)

Bạn cũng có thể sử dụng w+, nhưng điều này sẽ cắt ngắn (xóa) tất cả các nội dung hiện có.

Đây là một sơ đồ nhỏ đẹp từ một bài đăng khác:

Hướng dẫn how do you read and append a file in python? - làm thế nào để bạn đọc và nối một tệp trong python?

(source)

Cải thiện bài viết

Lưu bài viết

Trong khi đọc hoặc ghi vào một tệp, chế độ truy cập chi phối loại hoạt động có thể trong tệp đã mở. Nó đề cập đến cách các tập tin sẽ được sử dụng sau khi nó mở. Các chế độ này cũng xác định vị trí của xử lý tệp trong tệp. Định nghĩa của các chế độ truy cập này như sau:

  • Chỉ nối thêm (‘A,): Mở tệp để viết. Open the file for writing.
  • Nối và đọc (‘A+,): Mở tệp để đọc và viết. Open the file for reading and writing.

Khi tệp được mở ở chế độ nối trong Python, tay cầm được định vị ở cuối tệp. Dữ liệu được viết sẽ được chèn vào cuối, sau dữ liệu hiện có. & NBSP;

Ví dụ 1: Chương trình Python để minh họa Chế độ ghi vs Viết. Python program to illustrate Append vs write mode.

Python3

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
3
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
4
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
5
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
7
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
9
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
1
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
4
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
3
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
4
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
8
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
9

Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
0

Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
1

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
3
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
4
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
5
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
7
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
8
Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
9

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
0
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
1
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
1

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
3
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
4
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
5
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
7
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
Hello
This is Delhi
This is Paris
This is London
Today
0
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

Hello
This is Delhi
This is Paris
This is London
Today
2
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6
Hello
This is Delhi
This is Paris
This is London
Today
4
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

Hello
This is Delhi
This is Paris
This is London
Today
2
Hello
This is Delhi
This is Paris
This is London
Today
7

Hello
This is Delhi
This is Paris
This is London
Today
2
Hello
This is Delhi
This is Paris
This is London
Today
9

Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
1

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
3
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
4
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
5
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
7
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
9
Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
9

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
0a+0
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
1

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
3
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
4
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
5
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
7
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
Hello
This is Delhi
This is Paris
This is London
Today
0
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

Hello
This is Delhi
This is Paris
This is London
Today
2
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6w+3
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

Hello
This is Delhi
This is Paris
This is London
Today
2
Hello
This is Delhi
This is Paris
This is London
Today
7

Hello
This is Delhi
This is Paris
This is London
Today
2
Hello
This is Delhi
This is Paris
This is London
Today
9

Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
1

Output:

Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow

Ví dụ 2: & nbsp; nối dữ liệu từ một dòng mới  Append data from a new line

Trong ví dụ trên về xử lý tệp, có thể thấy rằng dữ liệu không được thêm vào dòng mới. Điều này có thể được thực hiện bằng cách viết ký tự mới ‘\ n, vào tệp. & Nbsp;

Python3

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
3
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
4
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
5
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
7
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
9
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
1
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
4
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
3
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
4
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
8
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
9

Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
0

Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
1

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
3
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
4
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
5
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
7
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
8
Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
9

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
0
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
18
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
0
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
21
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
0
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
24
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
3
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
4
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
5
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
7
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
Hello
This is Delhi
This is Paris
This is London
Today
0
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

Hello
This is Delhi
This is Paris
This is London
Today
2
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6
Hello
This is Delhi
This is Paris
This is London
Today
4
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

Hello
This is Delhi
This is Paris
This is London
Today
2
Hello
This is Delhi
This is Paris
This is London
Today
7

Hello
This is Delhi
This is Paris
This is London
Today
2
Hello
This is Delhi
This is Paris
This is London
Today
9

Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
1

Output:

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow

Lưu ý: ‘\ n, được coi là một đặc tính đặc biệt của hai byte. ‘\n’ is treated as a special character of two bytes.

Ví dụ 3: & nbsp; sử dụng với câu lệnh & nbsp; trong python  Using With statement  in Python

Với tuyên bố đượ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. Không giống như các triển khai ở trên, không cần phải gọi tệp.close () khi sử dụng với câu lệnh. Bản thân tuyên bố đảm bảo việc mua lại và phát hành tài nguyên thích hợp. & NBSP; is used in exception handling to make the code cleaner and much more readable. It simplifies the management of common resources like file streams. Unlike the above implementations, there is no need to call file.close() when using with statement. The with statement itself ensures proper acquisition and release of resources. 

Python3

with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
1
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
4
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
3
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
4
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
50____
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
9

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
52
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
5
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
7
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
9
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
58

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
59
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
0
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
61
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
59
Output of Readlines after appending
This is Delhi
This is Paris
This is LondonToday


Output of Readlines after writing
Tomorrow
0

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
52
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
5
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
7
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
70
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
58

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
59
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
0
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
21
with open("filename", "r+") as f:
    text = f.read()
    f.write("stuff to append")

    f.seek(0)  # return to the top of the file
    text = f.read()

    assert text.endswith("stuff to append")
0

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
52
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
5
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
6
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
7
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
8
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
81
with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
58

with open("filename", "a+") as f:
    # here, position is already at the end
    f.write("stuff to append")
59
Hello
This is Delhi
This is Paris
This is London
Today
2
Hello
This is Delhi
This is Paris
This is London
Today
7

Output:

Hello
This is Delhi
This is Paris
This is London
Today

LƯU Ý: Để biết thêm về tuyên bố bấm vào đây. To know more about with statement click here.