Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luậnopen[file_name, mode]
Parameters:
Hàm python open [] được sử dụng để mở [] các tệp được lưu trữ bên trong. Nó trả về nội dung của tệp dưới dạng các đối tượng Python. This parameter as the name suggests, is the name of the file that we want to open.
Cú pháp: Mở [File_Name, Mode] & NBSP;This parameter is a string that is used to specify the mode in which the file is to be opened. The following strings can be used to activate a specific mode:
- File_Name: Tham số này như tên gợi ý, là tên của tệp mà chúng tôi muốn mở. This string is used to read[only] the file. It is passed as default if no parameter is supplied and returns an error if no such file exists.
- Chế độ: Tham số này là một chuỗi được sử dụng để chỉ định chế độ mà tệp sẽ được mở. Các chuỗi sau có thể được sử dụng để kích hoạt một chế độ cụ thể:This string is used for writing on/over the file. If the file with the supplied name doesn’t exist, it creates one for you.
- Cấm Riến: Chuỗi này được sử dụng để đọc [chỉ] tệp. Nó được thông qua như mặc định nếu không có tham số nào được cung cấp và trả về lỗi nếu không có tệp đó tồn tại.This string is used to add[append] content to an existing file. If no such file exists, it creates one for you.
- Càng W W: Chuỗi này được sử dụng để ghi/trên tệp. Nếu tệp có tên được cung cấp không tồn tại, nó sẽ tạo một tệp cho bạn.This string is used to create a specific file.
- Một lần nữa, chuỗi này được sử dụng để thêm nội dung [nối] vào một tệp hiện có. Nếu không có tập tin như vậy tồn tại, nó sẽ tạo một tệp cho bạn. This string is used when the user wants to handle the file in binary mode. This is generally used to handle image files.
- X X X: Chuỗi này được sử dụng để tạo một tệp cụ thể.This string is used to handle files in text mode. By default, the open[] function uses the text mode.
Một lần nữa, chuỗi này được sử dụng khi người dùng muốn xử lý tệp ở chế độ nhị phân. Điều này thường được sử dụng để xử lý các tệp hình ảnh.
Cấm TTHER: Chuỗi này được sử dụng để xử lý các tệp ở chế độ văn bản. Theo mặc định, hàm Open [] sử dụng chế độ văn bản.
Python3
Ví dụ 1: Tạo tệp văn bản
Mã sau có thể được sử dụng để tạo một tệp. Ở đây chúng tôi sẽ tạo một tệp văn bản có tên là Geekforgeeks.txt.
Output:
True
created_file
=
Các
Ví dụ 2: Đọc và viết tệp
Ở đây chúng tôi sẽ viết chuỗi sau vào tệp GeekSforGeeks.txt mà chúng tôi vừa tạo và đọc lại cùng một tệp.
Python3
Geekforgeek là tốt nhất cho DSA
# opens test.text file of the current directory
f = open["test.txt"]
# specifying the full path
f = open["C:/Python33/README.txt"]
4# opens test.text file of the current directory
f = open["test.txt"]
# specifying the full path
f = open["C:/Python33/README.txt"]
5Geeksforgeeks is best for DSA3
# opens test.text file of the current directory
f = open["test.txt"]
# specifying the full path
f = open["C:/Python33/README.txt"]
7Mã dưới đây có thể được sử dụng cho cùng:
Geeksforgeeks is best for DSA4
# opens the file in reading mode
f = open["path_to_file", mode='r']
# opens the file in writing mode
f = open["path_to_file", mode = 'w']
# opens for writing to the end
f = open["path_to_file", mode = 'a']
7Output:
Geeksforgeeks is best for DSA
Các
Các
Python3
Ví dụ 3: Lắp đặt nội dung vào tệp
# opens test.text file of the current directory
f = open["test.txt"]
# specifying the full path
f = open["C:/Python33/README.txt"]
4f = open["path_to_file", mode = 'r', encoding='utf-8']
7Geeksforgeeks is best for DSA3
# opens test.text file of the current directory
f = open["test.txt"]
# specifying the full path
f = open["C:/Python33/README.txt"]
7Mã dưới đây có thể được sử dụng cho cùng:
Geeksforgeeks is best for DSA4
# opens the file in reading mode
f = open["path_to_file", mode='r']
# opens the file in writing mode
f = open["path_to_file", mode = 'w']
# opens for writing to the end
f = open["path_to_file", mode = 'a']
7Output:
Các
CácThe difference between “w” and “r” is that one overrides over the existing content whereas the latter adds content to the existing file keeping the content intact.
Hàm Open [] mở tệp [nếu có thể] và trả về đối tượng tệp tương ứng.
Cú pháp của =
0 là:
open[file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None]
mở [] tham số
- Tệp - đối tượng giống như đường dẫn [biểu thị đường dẫn hệ thống tệp] - path-like object [representing a file system path]
- Chế độ [Tùy chọn] - Chế độ trong khi mở tệp. Nếu không được cung cấp, nó mặc định là
=
1 [mở để đọc ở chế độ văn bản]. Các chế độ tệp có sẵn là: [optional] - mode while opening a file. If not provided, it defaults to=
1 [open for reading in text mode]. Available file modes are:Cách thứcSự mô tả =
1Mở một tập tin để đọc. [mặc định] =
3Mở một tập tin để viết. Tạo một tệp mới nếu nó không tồn tại hoặc cắt ngắn tệp nếu nó tồn tại. =
4Mở một tập tin để tạo độc quyền. Nếu tệp đã tồn tại, hoạt động không thành công. =
5Mở để nối thêm ở cuối tệp mà không cần cắt ngắn nó. Tạo một tệp mới nếu nó không tồn tại. =
6Mở ở chế độ văn bản. [mặc định] =
7Mở ở chế độ nhị phân. =
8Mở tệp để cập nhật [đọc và viết] - bộ đệm [tùy chọn] - được sử dụng để thiết lập chính sách bộ đệm [optional] - used for setting buffering policy
- Mã hóa [Tùy chọn] - Định dạng mã hóa [optional] - the encoding format
- Lỗi [Tùy chọn] - Chuỗi Chỉ định cách xử lý lỗi mã hóa/giải mã [optional] - string specifying how to handle encoding/decoding errors
- Newline [Tùy chọn] - Cách thức hoạt động của chế độ Newlines [giá trị có sẵn:
=
9,open
0,open
1,=
1 vàopen
3 [optional] - how newlines mode works [available values:=
9,open
0,open
1,=
1, andopen
3 - CloseFD [Tùy chọn] - Phải là
open
4 [mặc định]; Nếu được đưa ra khác, một ngoại lệ sẽ được nêu ra [optional] - must beopen
4 [default]; if given otherwise, an exception will be raised - mở [tùy chọn] - một người mở tùy chỉnh; Phải trả về một bộ mô tả tệp mở [optional] - a custom opener; must return an open file descriptor
Trả về giá trị từ Open []
Hàm =
0 Trả về một đối tượng tệp có thể được sử dụng để đọc, ghi và sửa đổi tệp.
Nếu không tìm thấy tệp, nó sẽ tăng ngoại lệ open
6.
Ví dụ 1: Làm thế nào để mở một tệp trong Python?
# opens test.text file of the current directory
f = open["test.txt"]
# specifying the full path
f = open["C:/Python33/README.txt"]
Vì chế độ bị bỏ qua, tệp được mở ở chế độ =
1; Mở ra để đọc.
Ví dụ 2: Cung cấp chế độ để mở []
# opens the file in reading mode
f = open["path_to_file", mode='r']
# opens the file in writing mode
f = open["path_to_file", mode = 'w']
# opens for writing to the end
f = open["path_to_file", mode = 'a']
Mã hóa mặc định của Python là ASCII. Bạn có thể dễ dàng thay đổi nó bằng cách truyền tham số open
8.
f = open["path_to_file", mode = 'r', encoding='utf-8']
Đọc được đề xuất: Đầu vào/đầu ra tệp Python Python File Input/Output