Hai trong số các chế độ được phép mở tệp Python là gì?

Python cũng hỗ trợ xử lý tệp và cho phép người dùng xử lý tệp i. e. , để đọc và ghi tệp, cùng với nhiều tùy chọn xử lý tệp khác, để thao tác trên tệp. Khái niệm xử lý tệp đã trải dài trên nhiều ngôn ngữ khác, nhưng việc triển khai phức tạp hoặc dài dòng, nhưng giống như các khái niệm khác của Python, khái niệm này ở đây cũng dễ dàng và ngắn gọn. Python xử lý các tệp khác nhau dưới dạng văn bản hoặc nhị phân và điều này rất quan trọng. Mỗi dòng mã bao gồm một dãy ký tự và chúng tạo thành một tệp văn bản. Mỗi dòng của tệp được kết thúc bằng một ký tự đặc biệt, được gọi là EOL hoặc ký tự Cuối dòng như dấu phẩy {,} hoặc ký tự xuống dòng. Nó kết thúc dòng hiện tại và báo cho trình thông dịch biết một dòng mới đã bắt đầu. Hãy bắt đầu với việc đọc và ghi tệp.  

Hoạt động của hàm open[]

Trước khi thực hiện bất kỳ thao tác nào trên tệp như đọc, ghi, trước hết ta phải mở tệp đó. Đối với điều này, chúng ta nên sử dụng hàm open[] sẵn có của Python nhưng tại thời điểm mở, chúng ta phải chỉ định chế độ, thể hiện mục đích của tệp mở

f = open[filename, mode]

Trường hợp chế độ sau được hỗ trợ

  1. r. mở một tệp hiện có cho thao tác đọc
  2. w. mở một tệp hiện có cho thao tác ghi. Nếu tệp đã chứa một số dữ liệu thì nó sẽ bị ghi đè nhưng nếu tệp không có thì nó cũng tạo tệp
  3. a. mở một tệp hiện có để nối thêm hoạt động. Nó sẽ không ghi đè dữ liệu hiện có
  4. r+. Để đọc và ghi dữ liệu vào tệp. Dữ liệu trước đó trong tệp sẽ bị ghi đè
  5. w+. Để ghi và đọc dữ liệu. Nó sẽ ghi đè dữ liệu hiện có
  6. một +. Để nối thêm và đọc dữ liệu từ tệp. Nó sẽ không ghi đè dữ liệu hiện có

Hãy xem ví dụ dưới đây

Python3




rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
3

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
5
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
6
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
8
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
9
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
0
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
0

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
1

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
2
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
3
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
6

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7____18
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
9

Lệnh open sẽ mở tệp ở chế độ đọc và vòng lặp for sẽ in từng dòng có trong tệp.  

Hoạt động của chế độ read[]

Có nhiều cách để đọc một tệp trong Python. Nếu bạn cần trích xuất một chuỗi chứa tất cả các ký tự trong tệp thì chúng ta có thể sử dụng tệp. đọc[]. Mã đầy đủ sẽ hoạt động như thế này.  

Python3




rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
30

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
5
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
6
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
35
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
9
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
37
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
0

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
8
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7____44
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
42

Một cách khác để đọc tệp là gọi một số ký tự nhất định như trong đoạn mã sau, trình thông dịch sẽ đọc năm ký tự đầu tiên của dữ liệu được lưu trữ và trả về dưới dạng chuỗi.  

Python3




rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
43

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
5
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
6
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
35
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
9
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
37
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
0

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
8
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4____455
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
56
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
57

Tạo tệp bằng chế độ write[]

Hãy xem cách tạo tệp và cách chế độ ghi hoạt động, vì vậy để thao tác với tệp, hãy viết phần sau trong môi trường Python của bạn.  

Python3




rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
58

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
5
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
6
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
8
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
9
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
65
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
0

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
68
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
69
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
0

_______44____468____473

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
0

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
76

Lệnh close[] chấm dứt tất cả các tài nguyên đang sử dụng và giải phóng hệ thống của chương trình cụ thể này.  

Hoạt động của chế độ append[]

Hãy cho chúng tôi xem chế độ chắp thêm hoạt động như thế nào.  

Python3




rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
77

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
5
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
6
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
8
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
9
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
84
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
0

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
68
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
88
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
0

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
76

Ngoài ra còn có nhiều lệnh khác trong xử lý tệp được sử dụng để xử lý các tác vụ khác nhau như.  

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.

Nó được thiết kế để cung cấp cú pháp rõ ràng hơn và xử lý ngoại lệ khi bạn đang làm việc với mã. Điều đó giải thích tại sao nên sử dụng chúng với một tuyên bố khi áp dụng. Điều này hữu ích vì sử dụng phương pháp này, bất kỳ tệp nào được mở sẽ tự động bị đóng sau khi thực hiện xong, do đó, tự động dọn dẹp.  

Ví dụ.  

Python3




rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
92

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
93______46
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
35
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
97
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
99

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7____501____45
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
04

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
05

Sử dụng write cùng với hàm with[]

Chúng ta cũng có thể sử dụng hàm write cùng với hàm  with[].  

Python3




rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
06

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
93______46
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
35
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
9
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
02
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
03

________ 17 ________ 105 ________ 106 ________ 10

split[] sử dụng xử lý tệp

Chúng ta cũng có thể tách dòng bằng xử lý tệp trong Python. Điều này phân chia biến khi gặp không gian. Bạn cũng có thể phân chia bằng bất kỳ ký tự nào theo ý muốn của chúng tôi. Đây là mã

Python3




rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
08

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
93
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
6
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
12____49
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
37
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
97
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
6

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7____501____45
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
22

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
7
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
2
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
25
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
4
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
27

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
28
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
29____45
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
31

rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
28
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
8
rstrip[]: This function strips each line of a file off spaces from the right-hand side.
lstrip[]: This function strips each line of a file off spaces from the left-hand side.
34

Ngoài ra còn có nhiều chức năng khác giúp thao tác với các tệp và nội dung của chúng. Người ta có thể khám phá nhiều chức năng khác trong Python Docs

Bài viết này được đóng góp bởi Chinmoy Lenka. Nếu bạn thích GeeksforGeeks và muốn đóng góp, bạn cũng có thể viết một bài báo bằng cách sử dụng write. chuyên viên máy tính. org hoặc gửi bài viết của bạn tới review-team@geeksforgeeks. tổ chức. Xem bài viết của bạn xuất hiện trên trang chính của GeeksforGeeks và trợ giúp các Geeks khác

Vui lòng viết nhận xét nếu bạn thấy bất cứ điều gì không chính xác hoặc nếu bạn muốn chia sẻ thêm thông tin về chủ đề đã thảo luận ở trên.  

Các chế độ mở tệp trong Python là gì?

Có 6 chế độ truy cập trong python. .
Chỉ đọc ['r']. Mở tệp văn bản để đọc. .
Đọc và Viết ['r+']. Mở tệp để đọc và ghi. .
Chỉ viết ['w']. Mở tệp để ghi. .
Viết và Đọc ['w+']. Mở tệp để đọc và ghi. .
Chỉ nối thêm ['a']. Mở tệp để ghi

Các chế độ có sẵn để mở tệp là gì?

Có nhiều chế độ mở file. .
r - mở tệp ở chế độ đọc
w - mở hoặc tạo tệp văn bản ở chế độ ghi
a - mở tệp ở chế độ chắp thêm
r+ - mở tệp ở cả chế độ đọc và ghi
a+ - mở tệp ở cả chế độ đọc và ghi
w+ - mở tệp ở cả chế độ đọc và ghi

Có bao nhiêu phương pháp để mở một tệp trong Python?

Trong Python, có sáu phương thức hoặc chế độ truy cập, đó là. Chỉ đọc ['r']. Chế độ này mở các tệp văn bản chỉ để đọc. Bắt đầu tệp là nơi đặt tay cầm. Nó làm tăng lỗi I/O nếu tệp không tồn tại.

Hai loại tệp trong Python là gì?

Có hai loại tệp dữ liệu chủ yếu — tệp văn bản và tệp nhị phân . Một tệp văn bản bao gồm các ký tự mà con người có thể đọc được, có thể được mở bởi bất kỳ trình soạn thảo văn bản nào. Mặt khác, các tệp nhị phân được tạo thành từ các ký tự và ký hiệu không thể đọc được của con người, yêu cầu các chương trình cụ thể để truy cập nội dung của nó.

Chủ Đề