Làm thế nào để bạn ghi vào một tệp hiện có trong python?

Trong khi đọc hoặc ghi vào tệp, chế độ truy cập sẽ chi phối loại thao tác có thể thực hiện trong tệp đã mở. Nó đề cập đến cách tệp sẽ được sử dụng sau khi được 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 để ghi
  • Nối và Đọc ['a+']. Mở tệp để đọc và ghi

Khi tệp được mở ở chế độ nối thêm trong Python, tay cầm được đặt ở cuối tệp. Dữ liệu được ghi sẽ được chèn vào cuối, sau dữ liệu hiện có.  

ví dụ 1. Chương trình Python để minh họa chế độ Nối và ghi

Python3




Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
6
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
7
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
8
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
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
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
2
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
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
7
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
3
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
4
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
1
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
6
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
1
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
8
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
9

Hello
This is Delhi
This is Paris
This is London
Today
0

Hello
This is Delhi
This is Paris
This is London
Today
1

 

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

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
6
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
7
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
8____49
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
Hello
This is Delhi
This is Paris
This is London
Today
9____460
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
61

_______462____463____00

Hello
This is Delhi
This is Paris
This is London
Today
1

 

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
6
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
7
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
8
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
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
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
72
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
74
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
9____476
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
74
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
79

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

Hello
This is Delhi
This is Paris
This is London
Today
1

 

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

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
6
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
7
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
8
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
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
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
2
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
60
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
92

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
62____494
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
0

Hello
This is Delhi
This is Paris
This is London
Today
1

 

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
6
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
7
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
8
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
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
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
72
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
74
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
9____507
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
74
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
79

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

Hello
This is Delhi
This is Paris
This is London
Today
1

đầu ra

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

ví dụ 2. Nối dữ liệu từ một dòng mới

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

Python3




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

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

 

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
6
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
7
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
8
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
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
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
2
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
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
7
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
3
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
4
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
1
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
6
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
1
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
8
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
9

Hello
This is Delhi
This is Paris
This is London
Today
0

Hello
This is Delhi
This is Paris
This is London
Today
1

 

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

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

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
6
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
7
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
8
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
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
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 London
TodayTomorrow
0

 

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

_______462____017____00

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
62____020
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
22

Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
62____024
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
6
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
7
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
8
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
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
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
72
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
74
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
9____476
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
74
Output of Readlines after appending
This is Delhi
This is Paris
This is London
TodayTomorrow
79

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

Hello
This is Delhi
This is Paris
This is London
Today
1

đầu ra

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

Ghi chú. '\ n' được coi là một ký tự đặc biệt gồm hai byte

ví dụ 3. Sử dụng câu lệnh With trong Python

với câu lệnh đượ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 phổ biến như luồng tệp. Không giống như các triển khai ở trên, không cần gọi tệp. close[] khi sử dụng với câu lệnh. Bản thân câu lệnh with đảm bảo việc thu thập và giải phóng tài nguyên phù hợp.  

Chủ Đề