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ợ
- r. mở một tệp hiện có cho thao tác đọc
- 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
- 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ó
- r+. Để đọc và ghi dữ liệu vào tệp. Dữ liệu trước đó trong tệp sẽ bị ghi đè
- w+. Để ghi và đọc dữ liệu. Nó sẽ ghi đè dữ liệu hiện có
- 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
# a file named "geek", will be opened with the reading mode.
file
=
open
[
'geek.txt'
,
'r'
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
file
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
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
# a file named "geek", will be opened with the reading mode.
0
file
=
open
[
# a file named "geek", will be opened with the reading mode.
5,
43_______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.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
[
file
44_______2Mộ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
file
3
file
=
open
[
# a file named "geek", will be opened with the reading mode.
5,
43_______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.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
[
file
=
5=
6=
7Tạ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
=
8
file
=
open
[
'geek.txt'
,
open
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.0
file
open
8_______46_______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
file
open
8_______47_______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.0
file
[
6
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
[
7
file
=
open
[
'geek.txt'
,
'geek.txt'
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.0
file
open
8_______48_______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.0
file
[
6
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.
Thí dụ.
Python3
,
2
,
3_______46_______[
# a file named "geek", will be opened with the reading mode.
5,
7file
,
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.7
'r'
1_______45_______ file
'r'
4'r'
5
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
'r'
6
,
3_______46_______[
# a file named "geek", will be opened with the reading mode.
5,
1_______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
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.05
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.0
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
,
3_______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.12_______49_______
# a file named "geek", will be opened with the reading mode.
7,
7file
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
'r'
1_______45_______ file
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
=
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.