Chúng tôi đã xem qua các thao tác khác nhau có thể được thực hiện trên một tệp bằng Python, như đọc, viết hoặc sao chép. Khi thực hiện bất kỳ thao tác xử lý tệp nào được đề cập ở trên, rõ ràng việc mở tệp là bước đầu tiên.
Vì vậy, hôm nay trong hướng dẫn này, chúng ta sẽ tập trung vào phần mở tệp bằng phương thức open[] của Python
Phương thức open[] trong Python
Phương thức
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation2 mở một tệp cụ thể ở chế độ đã chỉ định và trả về một đối tượng tệp. Đối tượng tệp này sau đó có thể được sử dụng thêm để thực hiện các thao tác tệp khác nhau. Cú pháp sử dụng phương thức được đưa ra dưới đây
open[file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None]
Nơi đây,
- tệp đề cập đến tên/bộ mô tả tệp và
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation
3 là chế độ mà tệp sẽ được mở. Đây là các tham số cơ bản cần thiết để mở tệp - đệm là một số nguyên tùy chọn được sử dụng để đặt chính sách đệm. Theo mặc định, nó được đặt thành [-1],
- mã hóa là tên của mã hóa được sử dụng để giải mã hoặc mã hóa tệp,
- lỗi là một chuỗi tùy chọn chỉ định cách xử lý lỗi mã hóa và giải mã. Lưu ý, điều này không thể được sử dụng trong chế độ nhị phân
- dòng mới kiểm soát cách hoạt động của chế độ dòng mới chung [chỉ áp dụng cho chế độ văn bản]. Nó có thể là
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation
4 [mặc định],# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation
5,# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation
6,# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation
0 và# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation
1 - closefd biểu thị xem tham số tệp đã truyền là tên tệp hay bộ mô tả tệp. Nó phải là Sai khi một bộ mô tả tệp được đề cập. Hoặc nếu không Đúng [mặc định]. Nếu không, một lỗi sẽ được nâng lên,
- công cụ mở là công cụ mở tùy chỉnh có thể gọi được. Bộ mô tả tệp được chỉ định cho đối tượng tệp có được bằng cách gọi
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation
2 này với [tệp, cờ]. trình mở phải trả về một bộ mô tả tệp đang mở [chuyển# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation
3 vì trình mở dẫn đến chức năng tương tự như chuyển# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation
4]
Các chế độ mở cho open[] trong Python
Các chế độ mở tệp khác nhau với ý nghĩa được đưa ra bên dưới
Các chế độMô tả# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation5mở để đọc [mặc định]
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation6mở để viết, cắt bớt tệp trước
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation7mở để tạo độc quyền, thất bại nếu tệp đã tồn tại
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation8mở để ghi, nối thêm vào cuối tệp nếu nó tồn tại
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation9chế độ nhị phân
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation00chế độ văn bản [mặc định]
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation01mở để cập nhật [đọc và viết]
Python open[] Ví dụ
Bây giờ chúng ta đã hoàn thành những kiến thức cơ bản về phương thức
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation2 trong Python, chúng ta hãy đi ngay vào một số ví dụ
Chúng ta sẽ mở một tệp có tên tệp. txt có nội dung [như hình bên dưới] bằng phương thức
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation2
Nhìn vào đoạn mã đưa ra dưới đây một cách cẩn thận
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation
đầu ra
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation0
Ở đây, chúng tôi đã mở tệp tệp. txt ở chế độ chỉ đọc [
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation04]. Phương thức
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation2 trả về một đối tượng tệp cho
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation06. Sau đó, chúng tôi đã lặp qua đối tượng này bằng cách sử dụng vòng lặp for để truy cập nội dung của tệp
Sau đó, chúng tôi đã đóng tệp bằng phương thức close[]. Điều quan trọng là phải đóng tệp ở cuối sau khi thực hiện bất kỳ thao tác nào trên tệp đó để tránh lỗi. Những lỗi này có thể xảy ra khi mở lại cùng một tệp
Mở nhiều tệp
Trong Python, chúng ta có thể mở đồng thời hai hoặc nhiều tệp bằng cách kết hợp câu lệnh
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation07, phương thức
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation2 và toán tử dấu phẩy[
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation09]. Hãy lấy một ví dụ để hiểu rõ hơn
Ở đây, chúng tôi đã thử mở hai tệp độc lập file1. txt và tệp2. txt và in nội dung tương ứng của chúng
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation8
đầu ra
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation9
Ghi chú. Chúng tôi đã không đóng các tập tin sau khi sử dụng thời gian này. Đó là vì chúng ta không cần, câu lệnh
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation07 đảm bảo rằng các tệp đã mở được đóng tự động bằng cách gọi phương thức
# opening a file f = open['file.txt', 'r'] # file object print["Type of f: ", type[f]] print["File contents:"] for i in f: print[i] f.close[] # closing file after successful operation81
Sự kết luận
Vì vậy, đó là nó cho ngày hôm nay. Hy vọng bạn đã có một sự hiểu biết rõ ràng. Đối với bất kỳ câu hỏi liên quan nào khác, vui lòng sử dụng các bình luận bên dưới