Trong khi thực hiện các thao tác với tệp, chúng tôi có thể cần nối thêm văn bản vào tệp hiện có mà không xóa dữ liệu hiện có. Trong bài viết này, chúng ta sẽ thảo luận về cách chúng ta có thể nối văn bản vào tệp trong python
Nối văn bản vào tệp bằng phương thức write[]
Để nối văn bản vào tệp bằng phương pháp
The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
0, trước tiên chúng ta cần mở tệp ở chế độ nối thêm. Đối với điều này, chúng ta sẽ sử dụng hàm The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
1 với tên tệp là tham số đầu tiên và “The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
2” là tham số thứ hai. Sau khi mở tệp, chúng ta chỉ cần thêm văn bản vào tệp bằng phương thức The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
0. Phương thức The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
0 được gọi trên một đối tượng tệp và lấy văn bản cần thêm vào tệp làm tham số đầu vào của nó. Bạn có thể quan sát toàn bộ quá trình này dưới đâymyFile = open["sample.txt", mode="r+"]
print["The content of the file before modification is:"]
text = myFile.read[]
print[text]
myString = "This string will be appended to the file."
myFile.write[myString]
myFile.close[]
myFile = open["sample.txt", "r"]
print["The content of the file after modification is:"]
text = myFile.read[]
print[text]
đầu ra
The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
Sau khi thêm văn bản vào tệp, đừng quên đóng tệp. Nếu không, nội dung sẽ không được lưu. Ở đây, chúng tôi đã sử dụng hàm read[] để xác minh nội dung của tệp trước và sau khi thêm văn bản
Nối văn bản vào tệp bằng hàm print[]
Thông thường, khi chúng ta sử dụng hàm
The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
5, nó sẽ in các giá trị ra đầu vào tiêu chuẩn. Tuy nhiên, chúng ta cũng có thể sử dụng hàm The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
5 để nối văn bản vào tệp trong python. Hàm The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
5 có tham số tùy chọn “The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
2”. Sử dụng tham số này, chúng ta có thể chỉ định nơi in các giá trị được truyền dưới dạng đầu vào cho hàm The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
5. Để nối thêm văn bản vào tệp, trước tiên chúng tôi sẽ mở tệp ở chế độ nối thêm bằng hàm
The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
1. Sau đó, chúng ta sẽ chuyển văn bản và đối tượng tệp cho hàm in làm đối số đầu vào thứ nhất và thứ hai tương ứng. Sau khi thực hiện chức năng The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
5, văn bản sẽ được thêm vào tệp. myFile = open["sample.txt", mode="r+"]
print["The content of the file before modification is:"]
text = myFile.read[]
print[text]
myString = "This string will be appended to the file."
print[myString, file=myFile]
myFile.close[]
myFile = open["sample.txt", "r"]
print["The content of the file after modification is:"]
text = myFile.read[]
print[text]
đầu ra
The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.
This string will be appended to the file.
Ở đầu ra, bạn có thể quan sát thấy rằng
The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
6 đã được thêm vào tệp trong một dòng mới. Khi chúng tôi thực hiện thao tác tương tự bằng phương pháp The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
0, The content of the file before modification is:
This is a sample file.
The content of the file after modification is:
This is a sample file.This string will be appended to the file.
6 đã được thêm vào dòng cuối cùng của tệp hiện có. Vì vậy, bạn có thể sử dụng sự khác biệt này để chọn phương pháp phù hợp theo yêu cầu của bạn. Ngoài ra, hãy đảm bảo rằng bạn đóng tệp sau khi thêm văn bản vào tệp. Nếu không, các thay đổi sẽ không được lưu. Sự kết luận
Trong bài viết này, chúng tôi đã thảo luận về hai cách để nối văn bản vào tệp trong python. Để tìm hiểu thêm về hoạt động của tệp, bạn có thể đọc bài viết này về xử lý tệp trong python. Bạn cũng có thể thích bài viết này về hiểu danh sách trong python
Có liên quan
Đào tạo Python được đề xuất
Khóa học. Python 3 cho người mới bắt đầu
Hơn 15 giờ nội dung video với hướng dẫn có hướng dẫn cho người mới bắt đầu. Tìm hiểu cách tạo các ứng dụng trong thế giới thực và nắm vững kiến thức cơ bản
Bạn có một hoặc nhiều tệp [ở định dạng văn bản hoặc định dạng giá trị được phân tách bằng dấu phẩy [csv]. Bạn muốn sử dụng một đoạn mã Python đơn giản để lập trình thêm văn bản vào [các] tệp của bạn
Thêm văn bản vào một tệp trong ví dụ về Python
Trong ví dụ đầu tiên của chúng tôi, chúng tôi sẽ chỉ ra cách bạn có thể dễ dàng mở một tệp hiện có và thêm một số văn bản vào đó. Trước tiên, chúng tôi sẽ xác định đường dẫn đến thư mục công việc và tên tệp. Sau đó xác định văn bản sẽ được thêm vào. Sau đó, chúng tôi sử dụng phương thức mở của TextIOWrapper và truy cập tệp ở chế độ nối thêm ['a'] và viết văn bản ở cuối tệp
import os
path_dir = Path['C:\WorkDir']
name_file = 'myfile.txt'
file_path = os.path.join[path_dir, name_file]
my_text = 'Copyright@2022'
with open [file_path, 'a'] as f:
f.write['\n'+ my_text ]
Đây là một đoạn mã tương tự, sử dụng thư viện đường dẫn để xác định đường dẫn đến tệp mà chúng tôi sẽ thay đổi
________số 8Nối vào tập tin nếu nó tồn tại
Để làm cho mã của chúng tôi mạnh mẽ hơn một chút, trước tiên chúng tôi sẽ tiếp tục và kiểm tra xem tệp csv hoặc txt mà chúng tôi muốn sửa đổi có tồn tại trong hệ thống tệp của chúng tôi không
from pathlib import Path
path_dir = Path['C:\WorkDir']
name_file = 'myfile.txt'
file_path = path_dir.joinpath[name_file]
my_text = 'Copyright@2022'
if file_path.is_file[]:
with open [file_path, 'a'] as f:
f.write['\n'+ my_text]
print['file modified']
else:
print['Not possible to append to non existing file']
Nối vào nhiều tệp văn bản hoặc tệp csv
Ví dụ cuối cùng của chúng tôi trong hướng dẫn này sẽ tập trung vào việc thêm một số dữ liệu vào nhiều tệp. Điều này sẽ rất tiện dụng, vì trong thế giới thực, rất có thể bạn sẽ sử dụng Python để sửa đổi đồng thời một số tệp và thư mục. Chúng tôi sẽ sử dụng thư viện toàn cầu để tìm các tệp cần sửa đổi. Trong ví dụ cụ thể của chúng tôi, chúng tôi sẽ tìm kiếm tất cả các lần xuất hiện của tệp csv trong một thư mục và thêm ghi chú bản quyền của chúng tôi