Hướng dẫn binary file handling programs in python - chương trình xử lý tệp nhị phân trong python

Trong Python, mô -đun IO cung cấp các phương thức của ba loại hoạt động IO; Các tập tin nhị phân thô, tệp nhị phân được đệm và tệp văn bản. Cách kinh điển để tạo một đối tượng tệp là bằng cách 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
2.

Bất kỳ hoạt động tệp nào cũng có thể được thực hiện trong ba bước sau:

  1. Mở tệp để lấy đối tượng tệp bằng hàm Open-in () 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 ().
  2. Thực hiện đọc, viết, phụ lục các hoạt động bằng cách sử dụng đối tượng tệp được lấy 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
    
    2.
  3. Đóng và xử lý đối tượng tệp.

Đọc tệp

Đối tượng tệp bao gồm các phương thức sau để đọc dữ liệu từ tệp.

  • Đọc (chars): Đọc số lượng ký tự được chỉ định bắt đầu từ vị trí hiện tại.
  • Readline (): Đọc các ký tự bắt đầu từ vị trí đọc hiện tại cho đến một ký tự mới.
  • Readlines (): Đọ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ệp.

This 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 phá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.

>>> 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 trong chế độ đọc mặc định từ thư mục hiện tại và trả về một đối tượng tệp. 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 char 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 rằng nhiều ký tự.
>>> f = open('C:\myfile.txt') # opening a file
>>> line1 = f.readline() # reading a line
>>> line1
'This is the first line. \n'
>>> line2 = f.readline() # reading a line
>>> line2
'This is the second line.\n'
>>> line3 = f.readline() # reading a line
>>> line3
'This is the third line.'
>>> line4 = f.readline() # reading a line
>>> line4
''
>>> f.close() # closing file object
0 sẽ xả và đóng luồng.

Đọc một dòng

Ví dụ sau đây cho thấy đọc một dòng từ tệp.

>>> f = open('C:\myfile.txt') # opening a file
>>> line1 = f.readline() # reading a line
>>> line1
'This is the first line. \n'
>>> line2 = f.readline() # reading a line
>>> line2
'This is the second line.\n'
>>> line3 = f.readline() # reading a line
>>> line3
'This is the third line.'
>>> line4 = f.readline() # reading a line
>>> line4
''
>>> f.close() # closing file object

Như bạn có thể thấy, chúng tôi phải mở tệp ở chế độ

>>> f = open('C:\myfile.txt') # opening a file
>>> line1 = f.readline() # reading a line
>>> line1
'This is the first line. \n'
>>> line2 = f.readline() # reading a line
>>> line2
'This is the second line.\n'
>>> line3 = f.readline() # reading a line
>>> line3
'This is the third line.'
>>> line4 = f.readline() # reading a line
>>> line4
''
>>> f.close() # closing file object
1. Phương thức
>>> f = open('C:\myfile.txt') # opening a file
>>> line1 = f.readline() # reading a line
>>> line1
'This is the first line. \n'
>>> line2 = f.readline() # reading a line
>>> line2
'This is the second line.\n'
>>> line3 = f.readline() # reading a line
>>> line3
'This is the third line.'
>>> line4 = f.readline() # reading a line
>>> line4
''
>>> f.close() # closing file object
2 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

Sau đây đọc tất cả các dòng bằng hàm

>>> f = open('C:\myfile.txt') # opening a file
>>> line1 = f.readline() # reading a line
>>> line1
'This is the first line. \n'
>>> line2 = f.readline() # reading a line
>>> line2
'This is the second line.\n'
>>> line3 = f.readline() # reading a line
>>> line3
'This is the third line.'
>>> line4 = f.readline() # reading a line
>>> line4
''
>>> f.close() # closing file object
3.

>>> f = open('C:\myfile.txt') # opening a file
>>> lines = f.readlines() # reading all lines
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close() # closing file object

Đối tượng tệp có một trình 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
>>> line1 = f.readline() # reading a line
>>> line1
'This is the first line. \n'
>>> line2 = f.readline() # reading a line
>>> line2
'This is the second line.\n'
>>> line3 = f.readline() # reading a line
>>> line3
'This is the third line.'
>>> line4 = f.readline() # reading a line
>>> line4
''
>>> f.close() # closing file object
4 được nâng lên, tức là, EOF đạt được.

f=open('C:\myfile.txt')
while True:
    try:
        line=next(f)
        print(line)
    except StopIteration:
        break
f.close()

Sử dụng vòng lặp để đọc một tập tin dễ dàng.

f=open('C:\myfile.txt')
for line in f:
    print(line)
f.close()

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 các tệp nhị phân, như được hiển thị bên dưới.

>>> f = open('C:\myimg.png', 'rb') # opening a binary file
>>> content = f.read() # reading all lines
>>> content
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x08\x00\x00\x00\x08\x08\x06
\x00\x00\x00\xc4\x0f\xbe\x8b\x00\x00\x00\x19tEXtSoftware\x00Adobe ImageReadyq
\xc9e\x00\x00\x00\x8dIDATx\xdab\xfc\xff\xff?\x03\x0c0/zP\n\xa4b\x818\xeco\x9c
\xc2\r\x90\x18\x13\x03*8\t\xc4b\xbc\x01\xa8X\x07$\xc0\xc8\xb4\xf0>\\\x11P\xd7?
\xa0\x84\r\x90\xb9\t\x88?\x00q H\xc1C\x16\xc9\x94_\xcc\x025\xfd2\x88\xb1\x04
\x88\x85\x90\x14\xfc\x05\xe2( \x16\x00\xe2\xc3\x8c\xc8\x8e\x84:\xb4\x04H5\x03
\xf1\\ .bD\xf3E\x01\x90\xea\x07\xe2\xd9\xaeB`\x82'
>>> f.close() # closing file object

Ghi vào một tệp

Đối tượng tệp cung cấp các phương thức sau để ghi vào một tệp.

  • Viết (các): Viết chuỗi S vào luồng và trả về số lượng ký tự được viết.
  • WriteLines (dòng): Viết một danh sách các dòng vào luồng. Mỗi dòng phải có một dải phân cách ở cuối của nó.

Tạo một tệp mới và ghi

Sau đây tạo ra 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ó.

>>> f = open('C:\myfile.txt','w')
>>> f.write("Hello") # writing to file
5
>>> f.close()

# reading file
>>> f = open('C:\myfile.txt','r') 
>>> f.read()
'Hello'
>>> f.close()

Trong ví dụ trên, câu lệnh

>>> f = open('C:\myfile.txt') # opening a file
>>> line1 = f.readline() # reading a line
>>> line1
'This is the first line. \n'
>>> line2 = f.readline() # reading a line
>>> line2
'This is the second line.\n'
>>> line3 = f.readline() # reading a line
>>> line3
'This is the third line.'
>>> line4 = f.readline() # reading a line
>>> line4
''
>>> 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ế độ 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
>>> line1 = f.readline() # reading a line
>>> line1
'This is the first line. \n'
>>> line2 = f.readline() # reading a line
>>> line2
'This is the second line.\n'
>>> line3 = f.readline() # reading a line
>>> line3
'This is the third line.'
>>> line4 = f.readline() # reading a line
>>> line4
''
>>> f.close() # closing file object
9.
>>> f = open('C:\myfile.txt') # opening a file
>>> lines = f.readlines() # reading all lines
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close() # closing file object
0 Chỉ định rằng tệp nên được ghi. Tiếp theo,
>>> f = open('C:\myfile.txt') # opening a file
>>> lines = f.readlines() # reading all lines
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close() # closing file object
1 ghi đè lên một 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ố lượng 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
>>> line1 = f.readline() # reading a line
>>> line1
'This is the first line. \n'
>>> line2 = f.readline() # reading a line
>>> line2
'This is the second line.\n'
>>> line3 = f.readline() # reading a line
>>> line3
'This is the third line.'
>>> line4 = f.readline() # reading a line
>>> line4
''
>>> f.close() # closing file object
0 đóng đối tượng tệp.

Nối vào một tệp hiện có

Sau đây, thêm nội dung ở cuối tệp hiện có bằng cách truyền chế độ

>>> f = open('C:\myfile.txt') # opening a file
>>> lines = f.readlines() # reading all lines
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close() # closing file object
4 hoặc
>>> f = open('C:\myfile.txt') # opening a file
>>> lines = f.readlines() # reading all lines
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close() # closing file object
5 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
2.

>>> f = open('C:\myfile.txt','a')
>>> f.write(" World!")
7
>>> f.close()

# reading file
>>> f = open('C:\myfile.txt','r') 
>>> f.read()
'Hello World!'
>>> f.close()

Viết nhiều dòng

Python cung cấp phương thức

>>> f = open('C:\myfile.txt') # opening a file
>>> lines = f.readlines() # reading all lines
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close() # closing file object
7 để lưu nội dung của đối tượng danh sách trong một tệp. Vì ký tự Newline không được tự động ghi vào tệp, nên nó phải được cung cấp như một phần của chuỗi.

>>> 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
0

Mở một tệp có 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" cho phép chỉ đọc và không viết. Để thực hiện các hoạt động đọc/nối đồng thời, hãy sử dụng chế độ "A+".

Viết vào một 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ở một tệp ở định dạng văn bản theo mặc định. Để mở một tệp ở định dạng nhị phân, thêm
>>> f = open('C:\myfile.txt') # opening a file
>>> lines = f.readlines() # reading all lines
>>> lines
'This is the first line. \nThis is the second line.\nThis is the third line.'
>>> f.close() # closing file object
9 vào tham số chế độ. Do đó, chế độ
f=open('C:\myfile.txt')
while True:
    try:
        line=next(f)
        print(line)
    except StopIteration:
        break
f.close()
0 mở tệp ở định dạng nhị phân để đọc, trong khi chế độ
f=open('C:\myfile.txt')
while True:
    try:
        line=next(f)
        print(line)
    except StopIteration:
        break
f.close()
1 mở tệp ở định dạng nhị phân để viết. Không giống như các tệp văn bản, các tệp nhị phân không thể đọc được của con người. Khi được 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 ra.

Mã sau đây lưu trữ một danh sách các số trong tệp nhị phân. Danh sách lần đầ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ề biểu diễn byte của đối tượ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
1