[Nhà tài trợ] Bắt đầu học Python với hướng dẫn Giới thiệu về Python miễn phí của DataCamp. Tìm hiểu Khoa học dữ liệu bằng cách hoàn thành các thử thách mã hóa tương tác và xem video của các chuyên gia hướng dẫn. Bắt đầu bây giờ
Cập nhật ngày 07 tháng 1 năm 2020
Trong bài đăng này, chúng ta sẽ tìm hiểu cách đọc và ghi tệp bằng Python
Làm việc với các tệp bao gồm ba bước sau
- Mở tệp tin
- Thực hiện thao tác đọc hoặc ghi
- Đóng tệp
Hãy xem chi tiết từng bước
Các loại tệp
Có hai loại tập tin
Tệp văn bản chỉ đơn giản là một tệp lưu trữ các chuỗi ký tự bằng mã hóa như utf-8, latin1, v.v. , trong khi trong trường hợp dữ liệu tệp nhị phân được lưu trữ ở định dạng giống như trong Bộ nhớ máy tính
Dưới đây là một số ví dụ về tệp văn bản và tệp nhị phân
tệp văn bản. Mã nguồn Python, tệp HTML, tệp văn bản, tệp đánh dấu, v.v.
tập tin nhị phân. tập tin thực thi, hình ảnh, âm thanh, vv
Điều quan trọng cần lưu ý là bên trong đĩa, cả hai loại tệp đều được lưu trữ dưới dạng chuỗi 1 và 0. Sự khác biệt duy nhất là khi một tệp văn bản được mở, dữ liệu được giải mã trở lại bằng cách sử dụng cùng một sơ đồ mã hóa mà chúng đã được mã hóa trong. Tuy nhiên, trong trường hợp tệp nhị phân, điều đó sẽ không xảy ra
Mở tệp - hàm open[]
Chức năng tích hợp sẵn của
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>7 được sử dụng để mở tệp. Cú pháp của nó như sau
open[filename, mode] -> file object
Khi thành công,
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>7 trả về một đối tượng tệp. Khi thất bại, nó tăng
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>9 hoặc nó là phân lớp
Đối sốMô tả
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 230Đường dẫn tuyệt đối hoặc tương đối của tệp sẽ được mở. Chế độ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 231[tùy chọn] là một chuỗi đề cập đến chế độ xử lý [i. e đọc, viết, chắp thêm, v.v.;] và loại tệp
Sau đây là các giá trị có thể có của chế độ
Chế độ Mô tả
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 232Mở tệp để đọc [mặc định].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 233Mở tệp để viết.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 234Mở tệp ở chế độ chắp thêm i. e thêm dữ liệu mới vào cuối file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 235Mở tệp để đọc và ghi cả
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 236Mở tệp để ghi, chỉ khi nó chưa tồn tại
Chúng tôi cũng có thể thêm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 237 hoặc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 238 vào chuỗi chế độ để cho biết loại tệp mà chúng tôi sẽ làm việc với.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 237 được sử dụng cho tệp văn bản và
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 238 cho tệp nhị phân. Nếu không được chỉ định,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 237 được giả định theo mặc định
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 231 là tùy chọn, nếu không được chỉ định thì tệp sẽ được mở dưới dạng tệp văn bản chỉ để đọc
Điều này có nghĩa là ba cuộc gọi sau tới
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>7 là tương đương
open[filename, mode] -> file object8
Lưu ý rằng trước khi bạn có thể đọc một tệp, tệp đó phải tồn tại, nếu không thì
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>7 sẽ tăng ngoại lệ
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>05. Tuy nhiên, nếu bạn mở một tệp để ghi [sử dụng chế độ như
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 233,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 234 hoặc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 235], Python sẽ tự động tạo tệp cho bạn. Nếu tệp đã tồn tại thì nội dung của nó sẽ bị xóa. Nếu bạn muốn ngăn điều đó, hãy mở tệp ở chế độ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 236
Đóng tệp - phương thức close[]
Khi bạn làm việc xong với tệp, bạn nên đóng tệp. Mặc dù, tệp sẽ tự động đóng khi chương trình kết thúc nhưng vẫn nên làm như vậy một cách rõ ràng. Không thể đóng tệp trong một chương trình lớn có thể gây ra sự cố và thậm chí có thể khiến chương trình bị lỗi
Để đóng tệp, gọi phương thức
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>50 của đối tượng tệp. Đóng tệp sẽ giải phóng các tài nguyên được liên kết với nó và xóa dữ liệu trong bộ đệm vào đĩa
Con trỏ tệp
Khi bạn mở tệp qua phương thức
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>7. Hệ điều hành liên kết một con trỏ trỏ đến một ký tự trong tệp. Con trỏ tệp xác định nơi diễn ra thao tác đọc và ghi. Ban đầu, con trỏ tệp chỉ vào đầu tệp và tăng dần khi chúng ta đọc và ghi dữ liệu vào tệp. Ở phần sau của bài đăng này, chúng ta sẽ xem cách xác định vị trí hiện tại của con trỏ tệp và sử dụng nó để truy cập ngẫu nhiên các phần của tệp
Đọc tệp bằng read[], readline[] và readlines[]
Để đọc dữ liệu, đối tượng tệp cung cấp các phương thức sau
MethodArgument
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>52Đọc và trả về
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>53 byte trở xuống [nếu không có đủ ký tự để đọc] từ tệp dưới dạng chuỗi. Nếu
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>53 không được chỉ định, nó sẽ đọc toàn bộ tệp dưới dạng một chuỗi và trả về.
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>55Đọc và trả về các ký tự cho đến khi đạt đến cuối dòng dưới dạng chuỗi.
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>56Đọc và trả về tất cả các dòng dưới dạng danh sách các chuỗi
Khi đến cuối tệp [EOF], các phương thức
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>57 và
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>55 trả về một chuỗi rỗng, trong khi đó,
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>56 trả về một danh sách trống [
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>50]
Dưới đây là một số ví dụ
bài thơ. txt
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>6
ví dụ 1. Sử dụng
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>57
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>8
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>
ví dụ 2. Sử dụng
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>0
ví dụ 3. Sử dụng
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>56
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>5
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>5
Đọc tệp theo khối
Các phương thức
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>57 [không có đối số] và
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>56 đọc tất cả dữ liệu vào bộ nhớ cùng một lúc. Vì vậy, đừng sử dụng chúng để đọc các tệp lớn
Cách tiếp cận tốt hơn là đọc tệp theo khối bằng cách sử dụng
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>57 hoặc đọc từng dòng tệp bằng cách sử dụng
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>55, như sau
Thí dụ. Đọc tệp theo khối
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 230
open[filename, mode] -> file object80
Thí dụ. Đọc từng dòng tệp
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>8
open[filename, mode] -> file object82
Thay vì sử dụng các phương thức
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>57 [có đối số] hoặc
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>55, bạn cũng có thể sử dụng đối tượng tệp để lặp lại nội dung của tệp mỗi lần một dòng
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>5
open[filename, mode] -> file object84
Mã này tương đương với ví dụ trước nhưng ngắn gọn hơn, dễ đọc và dễ gõ hơn
cảnh báo
Hãy cẩn thận với phương pháp
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>55, nếu bạn không may mở một tệp lớn mà không có bất kỳ dòng mới nào thì
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>55 không tốt hơn
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>57 [không có đối số]. Điều này cũng đúng khi bạn sử dụng đối tượng tệp làm trình vòng lặp
Viết dữ liệu bằng cách sử dụng write[] và writelines[]
Để ghi dữ liệu, đối tượng tệp cung cấp hai phương thức sau
MethodDescription
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2303Ghi chuỗi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2304 vào tệp và trả về số ký tự đã viết.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2305Ghi tất cả các chuỗi trong chuỗi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2304 vào tệp
Dưới đây là các ví dụ
open[filename, mode] -> file object85
open[filename, mode] -> file object86
Lưu ý rằng không giống như hàm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2307, phương thức
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2308 không thêm ký tự xuống dòng [
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2309] ở cuối dòng. Nếu bạn muốn ký tự xuống dòng, bạn phải thêm ký tự đó theo cách thủ công, như sau
open[filename, mode] -> file object87
open[filename, mode] -> file object88
Bạn cũng có thể thêm dòng mới vào dòng bằng hàm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2307, như sau
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>60
Đây là một ví dụ về phương pháp
open[filename, mode] -> file object801
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>61
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>62
Phương thức
open[filename, mode] -> file object801 gọi nội bộ phương thức
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2308
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>63
Đây là một ví dụ khác mở tệp ở chế độ chắp thêm
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>61
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>65
Giả sử tệp
open[filename, mode] -> file object804 rất quan trọng để sử dụng và chúng tôi không muốn nó bị ghi đè. Để ngăn chặn điều đó, hãy mở tệp ở chế độ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 236
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>66
Chế độ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 236 chỉ mở tệp để ghi, nếu nó chưa tồn tại
Buffering và Flushing
Bộ đệm là quá trình lưu trữ dữ liệu tạm thời trước khi nó được chuyển đến một vị trí mới
Trong trường hợp tệp, dữ liệu không được ghi ngay vào đĩa mà được lưu trữ trong bộ nhớ đệm
Cơ sở lý luận đằng sau việc này là việc ghi dữ liệu vào đĩa cần có thời gian thay vì ghi dữ liệu vào bộ nhớ vật lý. Hãy tưởng tượng một chương trình ghi dữ liệu mỗi khi phương thức
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2308 được gọi. Một chương trình như vậy sẽ rất chậm
Khi chúng ta sử dụng bộ đệm, dữ liệu chỉ được ghi vào đĩa khi bộ đệm đầy hoặc khi phương thức
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>50 được gọi. Quá trình này được gọi là xả đầu ra. Bạn cũng có thể xóa đầu ra theo cách thủ công bằng cách sử dụng phương thức
open[filename, mode] -> file object809 của đối tượng tệp. Lưu ý rằng
open[filename, mode] -> file object809 chỉ lưu dữ liệu được đệm vào đĩa. Nó không đóng tập tin
Phương thức
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>7 cung cấp đối số thứ ba tùy chọn để kiểm soát bộ đệm. Để tìm hiểu thêm về nó, hãy truy cập tài liệu chính thức
Đọc và ghi dữ liệu nhị phân
Việc đọc và ghi tệp nhị phân được thực hiện bằng cách nối thêm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 238 vào chuỗi chế độ
Trong Python 3, dữ liệu nhị phân được biểu diễn bằng một loại đặc biệt có tên là
open[filename, mode] -> file object813
Loại
open[filename, mode] -> file object813 đại diện cho một dãy số bất biến trong khoảng từ 0 đến 255
Hãy tạo một phiên bản nhị phân của bài thơ bằng cách đọc tệp
open[filename, mode] -> file object815
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>67
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>68
Lưu ý rằng việc lập chỉ mục một đối tượng
open[filename, mode] -> file object813 sẽ trả về một
open[filename, mode] -> file object817
Hãy viết bài thơ nhị phân của chúng ta trong một tệp mới
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>69
Bài thơ nhị phân của chúng tôi bây giờ được ghi vào tập tin. Để đọc nó, hãy mở tệp ở chế độ
open[filename, mode] -> file object818
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>80
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>81
Điều quan trọng cần lưu ý là, trong trường hợp của chúng tôi, dữ liệu nhị phân có chứa các ký tự có thể in được, như bảng chữ cái, dòng mới, v.v. Tuy nhiên, đây sẽ không phải là trường hợp hầu hết thời gian. Điều đó có nghĩa là với dữ liệu nhị phân, chúng tôi không thể sử dụng một cách đáng tin cậy
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>55 và đối tượng tệp [dưới dạng trình lặp] để đọc nội dung của tệp vì có thể không có ký tự xuống dòng trong tệp. Cách tốt nhất để đọc dữ liệu nhị phân là đọc nó theo từng đoạn bằng phương pháp
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>57
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 230
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>83
Truy cập ngẫu nhiên bằng fseek[] và ftell[]
Trước đó trong bài đăng này, chúng tôi đã biết rằng khi tệp được mở, hệ thống sẽ liên kết một con trỏ với nó, xác định vị trí sẽ diễn ra quá trình đọc hoặc ghi
Cho đến nay chúng tôi đã đọc và ghi các tập tin một cách tuyến tính. Nhưng cũng có thể đọc và viết tại các vị trí cụ thể. Để đạt được điều này, đối tượng tệp cung cấp hai phương thức sau
MethodDescription
open[filename, mode] -> file object821Trả về vị trí hiện tại của con trỏ tệp.
open[filename, mode] -> file object822Di chuyển con trỏ tệp đến
open[filename, mode] -> file object823 đã cho.
open[filename, mode] -> file object823 đề cập đến số byte và
open[filename, mode] -> file object825 xác định vị trí liên quan mà
open[filename, mode] -> file object823 sẽ di chuyển con trỏ tệp. Giá trị mặc định của
open[filename, mode] -> file object825 là 0, nghĩa là offset sẽ di chuyển con trỏ tệp từ đầu tệp. Nếu wherece được đặt thành
open[filename, mode] -> file object828 hoặc
open[filename, mode] -> file object829, phần bù sẽ di chuyển con trỏ của tệp từ vị trí hiện tại hoặc từ cuối tệp, tương ứng
Bây giờ chúng ta hãy lấy một số ví dụ
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>84
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>85
Sau khi đọc hết 5 ký tự, con trỏ tệp hiện tại ký tự
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 234 [trong word
open[filename, mode] -> file object831]. Vì vậy thao tác đọc [hoặc ghi] tiếp theo sẽ bắt đầu từ thời điểm này
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>8
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>87
Bây giờ chúng tôi đã đến cuối tập tin. Lúc này ta có thể dùng phương thức
open[filename, mode] -> file object832 để tua con trỏ file về đầu file như sau
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>88
Con trỏ tệp hiện đang ở đầu tệp. Tất cả các thao tác đọc và ghi từ bây giờ sẽ diễn ra lại từ đầu tệp
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>89
Để di chuyển con trỏ tệp từ 12 byte về phía trước từ vị trí hiện tại, hãy gọi
open[filename, mode] -> file object833 như sau
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>0
Con trỏ tệp lúc này đang ở ký tự
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 234 [sau từ
open[filename, mode] -> file object835] nên thao tác đọc ghi sẽ diễn ra từ đó
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>1
Chúng ta cũng có thể di chuyển con trỏ tệp về phía sau. Ví dụ: lệnh gọi sau tới ____1833 di chuyển con trỏ tệp lùi 13 byte so với vị trí hiện tại
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>2
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>3
Giả sử chúng ta muốn đọc 16 byte cuối cùng của tệp. Để làm như vậy, hãy di chuyển con trỏ tệp về phía sau 16 byte so với cuối tệp
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>4
Các giá trị của đối số
open[filename, mode] -> file object825 của
open[filename, mode] -> file object832 cũng được định nghĩa là hằng số trong mô-đun
open[filename, mode] -> file object839
Giá trịConstant
open[filename, mode] -> file object840
open[filename, mode] -> file object841
open[filename, mode] -> file object828
open[filename, mode] -> file object843____1829
open[filename, mode] -> file object845
với tuyên bố
Câu lệnh with cho phép chúng ta tự động đóng tệp sau khi làm việc xong với nó. Cú pháp của nó như sau
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>5
Các câu lệnh bên trong câu lệnh
open[filename, mode] -> file object835 phải được thụt lề giống như vòng lặp for, nếu không thì ngoại lệ
open[filename, mode] -> file object847 sẽ được nâng lên
Đây là một ví dụ
>>> >>> f = open["poem.txt", "r"] >>> >>> f.read[3] # read the first 3 characters 'The' >>> >>> f.read[] # read the remaining characters in the file. ' caged bird sings\nwith a fearful trill\nof things unknown\nbut longed for still\n' >>> >>> f.read[] # End of the file [EOF] is reached '' >>> >>> f.close[] >>>6
Hướng dẫn khác [Nhà tài trợ]
Trang web này được hỗ trợ rộng rãi bởi DataCamp. DataCamp cung cấp Hướng dẫn Python tương tác trực tuyến cho Khoa học dữ liệu. Tham gia cùng hơn một triệu người học khác và bắt đầu học Python cho khoa học dữ liệu ngay hôm nay