Trong Python, mô-đun cung cấp các phương thức của ba loại hoạt động IO; . Cách chính tắc để tạo một đối tượng tệp là sử dụng hàm
>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
2Mọi thao tác với tệp có thể được thực hiện theo ba bước sau
- Mở tệp để lấy đối tượng tệp bằng hàm open[] tích hợp. Có các chế độ truy cập khác nhau mà bạn có thể chỉ định trong khi mở tệp bằng hàm open[]
- Thực hiện các thao tác đọc, ghi, chắp thêm bằng cách sử dụng đối tượng tệp được lấy từ hàm
2>>> f = open['C:\myfile.txt'] # opening a file >>> lines = f.read[] # reading a file >>> lines 'This is the first line. \nThis is the second line.\nThis is the third line.' >>> f.close[] # closing file object
- Đóng và xử lý đối tượng tệp
Đọc tập tin
Đối tượng tệp bao gồm các phương thức sau để đọc dữ liệu từ tệp
- đọc [ký tự]. đọc số lượng ký tự được chỉ định bắt đầu từ vị trí hiện tại
- dòng đọc []. đọc các ký tự bắt đầu từ vị trí đọc hiện tại cho đến một ký tự xuống dòng
- đường đọc []. đọc tất cả các dòng cho đến khi kết thúc tệp và trả về một đối tượng danh sách
Tệp
>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
4 sau đây sẽ được sử dụng trong tất cả các ví dụ về đọc và ghi tệpC. \tập tin của tôi. txt
Sao chépThis is the first line.
This is the second line.
This is the third line.
Ví dụ sau thực hiện thao tác đọc bằng phương thức
>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
5Ví dụ. Đọc một tập tin
Sao chép>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
Ở trên,
>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
6 mở >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
7 ở chế độ đọc mặc định từ thư mục hiện tại và trả về một. Hàm >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
8 đọc tất cả nội dung cho đến khi EOF dưới dạng chuỗi. Nếu bạn chỉ định đối số kích thước ký tự trong phương thức >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
5, thì nó sẽ chỉ đọc nhiều ký tự đó. >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
00 sẽ xả và đóng luồngđọc một dòng
Ví dụ sau minh họa việc đọc một dòng từ tệp
Ví dụ. đọc dòng
Sao chép>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
0Như bạn có thể thấy, chúng tôi phải mở tệp ở chế độ
>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
01. Phương thức >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
02 sẽ trả về dòng đầu tiên và sau đó sẽ trỏ đến dòng thứ hai trong tệpĐọc tất cả các dòng
Phần sau đọc tất cả các dòng bằng hàm
>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
03Ví dụ. Đọc một tập tin
Sao chép>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
5Đối tượng tệp có một trình vòng lặp sẵn có. Chương trình sau đây đọc từng dòng tệp đã cho cho đến khi
>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
04 được nâng lên, i. e. , EOF đạt đượcVí dụ. Trình lặp tệp
Sao chép>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
7Sử dụng vòng lặp for để đọc tệp dễ dàng
Ví dụ. Đọc tệp bằng vòng lặp For
Sao chép>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
8đầu ra
This is the first line.
This is the second line.
This is the third line.
Đọc tệp nhị phân
Sử dụng chế độ 'rb' trong hàm
>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
2 để đọc tệp nhị phân, như hình bên dướiVí dụ. Đọc một tập tin
Sao chép>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
1Viết vào một tập tin
Đối tượng tệp cung cấp các phương thức sau để ghi vào tệp
- viết [s]. Viết chuỗi s vào luồng và trả về số ký tự đã viết
- dòng viết [dòng]. Viết danh sách các dòng vào luồng. Mỗi dòng phải có dấu phân cách ở cuối dòng
Tạo một tệp mới và ghi
Sau đây tạo một tệp mới nếu nó không tồn tại hoặc ghi đè lên một tệp hiện có
Ví dụ. Tạo hoặc ghi đè lên tệp hiện có
>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
2Trong ví dụ trên, câu lệnh
>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
06 mở >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
7 ở chế độ ghi, phương thức >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
2 trả về đối tượng tệp và gán nó cho một biến >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
09. >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
50 chỉ định rằng tệp có thể ghi được. Tiếp theo, >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
51 ghi đè nội dung hiện có của tệp >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
7. Nó trả về số ký tự được ghi vào một tệp, là 5 trong ví dụ trên. Cuối cùng, >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
00 đóng đối tượng tệpNối vào một tệp hiện có
Phần sau nối nội dung vào cuối tệp hiện có bằng cách chuyển chế độ
>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
54 hoặc >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
55 trong phương thức >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
2Ví dụ. Nối vào tệp hiện có
Sao chépThis is the first line.
This is the second line.
This is the third line.
4Viết nhiều dòng
Python cung cấp phương thức
>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
57 để lưu nội dung của đối tượng danh sách vào một tệp. Vì ký tự dòng mới không được ghi tự động vào tệp nên nó phải được cung cấp như một phần của chuỗiVí dụ. Viết dòng vào tập tin
Sao chép>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
0Việc mở tệp bằng chế độ "w" hoặc chế độ "a" chỉ có thể được ghi vào và không thể đọc được từ. Tương tự, chế độ "r" chỉ cho phép đọc và không ghi. Để thực hiện các thao tác đọc/chắp thêm đồng thời, hãy sử dụng chế độ "a+"
Ghi vào tệp nhị phân
Hàm
>>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
2 mở tệp ở định dạng văn bản theo mặc định. Để mở tệp ở định dạng nhị phân, hãy thêm >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
59 vào tham số chế độ. Do đó, chế độ >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
70 mở tệp ở định dạng nhị phân để đọc, trong khi chế độ >>> f = open['C:\myfile.txt'] # opening a file
>>> lines = f.read[] # reading a file
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close[] # closing file object
71 mở tệp ở định dạng nhị phân để ghi. Không giống như tệp văn bản, tệp nhị phân không thể đọc được bằng con người. Khi mở bằng bất kỳ trình soạn thảo văn bản nào, dữ liệu không thể nhận dạng đượcĐoạn mã sau lưu trữ danh sách các số trong tệp nhị phân. Danh sách đầu tiên được chuyển đổi trong một mảng byte trước khi viết. Hàm tích hợp bytearray[] trả về một biểu diễn byte của đối tượng