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:
[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"]
3with 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"]
5with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
7with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
9with 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"]
0with 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"]
1with 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"]
3with 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"]
4with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8with 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"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8with 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"]
8with 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"]
9Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow0
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow1
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
3with 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"]
5with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
7with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow8
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow9
Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow0
Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow1
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"]
0Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow1
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
3with 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"]
5with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
7with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8Hello This is Delhi This is Paris This is London Today0
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"]
0Hello This is Delhi This is Paris This is London Today2
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6Hello This is Delhi This is Paris This is London Today4
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"]
0Hello This is Delhi This is Paris This is London Today2
Hello This is Delhi This is Paris This is London Today7
Hello This is Delhi This is Paris This is London Today2
Hello This is Delhi This is Paris This is London Today9
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow1
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
3with 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"]
5with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
7with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
9Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow9
Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow0
a+
0with 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"]
0Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow1
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
3with 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"]
5with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
7with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8Hello This is Delhi This is Paris This is London Today0
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"]
0Hello This is Delhi This is Paris This is London Today2
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6w+
3with 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"]
0Hello This is Delhi This is Paris This is London Today2
Hello This is Delhi This is Paris This is London Today7
Hello This is Delhi This is Paris This is London Today2
Hello This is Delhi This is Paris This is London Today9
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow1
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"]
3with 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"]
5with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
7with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
9with 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"]
0with 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"]
1with 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"]
3with 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"]
4with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8with 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"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8with 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"]
8with 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"]
9Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow0
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow1
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
3with 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"]
5with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
7with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow8
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow9
Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow0
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
18with 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"]
0Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow0
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
21with 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"]
0Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow0
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
24with 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"]
0with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
3with 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"]
5with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
7with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8Hello This is Delhi This is Paris This is London Today0
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"]
0Hello This is Delhi This is Paris This is London Today2
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6Hello This is Delhi This is Paris This is London Today4
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"]
0Hello This is Delhi This is Paris This is London Today2
Hello This is Delhi This is Paris This is London Today7
Hello This is Delhi This is Paris This is London Today2
Hello This is Delhi This is Paris This is London Today9
Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow1
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"]
1with 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"]
3with 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"]
4with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8with 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"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8with 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"]
9with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
52with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
5with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
7with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
9with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
58with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
59Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow0
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
61with 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"]
0with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
59Output of Readlines after appending This is Delhi This is Paris This is LondonToday Output of Readlines after writing Tomorrow0
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
52with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
5with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
7with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
70with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
58with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
59Output of Readlines after appending This is Delhi This is Paris This is London TodayTomorrow0
with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
21with 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"]
0with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
52with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
5with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
6with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
7with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
8with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
81with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
58with open["filename", "a+"] as f:
# here, position is already at the end
f.write["stuff to append"]
59Hello This is Delhi This is Paris This is London Today2
Hello This is Delhi This is Paris This is London Today7
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.