Đọc và ghi tệp Python

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
2

Mọi thao tác với tệp có thể được thực hiện theo ba bước sau

  1. 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[]
  2. 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
    >>> 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 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ệp

C. \tập tin của tôi. txt

Sao ché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 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í 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
>>> 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óa 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

Thí dụ. đọc dòng

Sao ché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

Phần 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

Thí dụ. Đọc một tập tin

Sao chép

>>> 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 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
>>> 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ôi. e. , EOF đạt được

Thí dụ. Trình lặp tệp

Sao chép

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 for để đọc tệp dễ dàng

Thí dụ. Đọc tệp bằng vòng lặp For

Sao chép

f=open['C:\myfile.txt']
for line in f:
    print[line]
f.close[]

đầ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ưới

Thí dụ. Đọc một tập tin

Sao chép

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

Viế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ó

Thí dụ. Tạo hoặc ghi đè lên tệp hiện có

Sao chép

>>> 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 có thể ghi được. 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 đè 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
>>> 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ó

Phần sau nối thêm 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.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

Thí dụ. Nối vào tệp hiện có

Sao chép

>>> 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 vào 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ỗi

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

Việ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.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 thông 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 để 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

Chế độ đọc và ghi trong Python là gì?

Trong khi ghi vào tệp bằng Python, bạn cần mở tệp ở chế độ ghi. Dưới đây là một số chức năng trong Python cho phép bạn đọc và ghi vào tệp. đọc[]. Hàm này đọc toàn bộ tệp và trả về một chuỗi . readline[]. Hàm này đọc các dòng từ tệp đó và trả về dưới dạng một chuỗi .

Tệp đã đọc [] trong Python là gì?

Phương thức đọc tệp Python[] . Mặc định là -1 có nghĩa là toàn bộ tập tin. returns the specified number of bytes from the file. Default is -1 which means the whole file.

Chủ Đề