Python cung cấp các hàm sẵn có để tạo, viết và đọc tệp. Có hai loại tệp có thể được xử lý trong python, tệp văn bản bình thường và tệp nhị phân (được viết bằng ngôn ngữ nhị phân, 0 và 1) Show
Trong bài viết này, chúng tôi sẽ tập trung vào việc mở, đóng, đọc và ghi dữ liệu trong một tệp văn bản Chế độ truy cập tệp Chế độ truy cập chi phối loại hoạt động có thể có trong tệp đã mở. Nó đề cập đến cách tệp sẽ được sử dụng sau khi được mở. Các chế độ này cũng xác định vị trí của Xử lý tệp trong tệp. Bộ xử lý tệp giống như một con trỏ, xác định vị trí dữ liệu phải được đọc hoặc ghi trong tệp. Có 6 chế độ truy cập trong python
Cách tệp được tải vào bộ nhớ chính Có hai loại bộ nhớ trong máy tính. e. Bộ nhớ chính và bộ nhớ phụ mọi tệp bạn đã lưu hoặc bất kỳ ai đã lưu đều nằm trên bộ nhớ phụ khiến mọi dữ liệu trong bộ nhớ chính bị xóa khi tắt máy tính. Vì vậy, khi bạn cần thay đổi bất kỳ tệp văn bản nào hoặc chỉ để làm việc với chúng trong python, bạn cần tải tệp đó vào bộ nhớ chính. Python tương tác với các tệp được tải trong bộ nhớ chính hoặc bộ nhớ chính thông qua “trình xử lý tệp” ( Đây là cách hệ điều hành của bạn cấp quyền truy cập cho python để tương tác với tệp bạn đã mở bằng cách tìm kiếm tệp trong bộ nhớ của nó nếu tìm thấy, nó sẽ trả về một trình xử lý tệp và sau đó Mở một tệp Nó được thực hiện bằng hàm open(). Không cần nhập mô-đun cho chức năng này File_object = open(r"File_Name","Access_Mode") Tệp phải tồn tại trong cùng thư mục với tệp chương trình python khác, địa chỉ đầy đủ của tệp phải được ghi thay cho tên tệp. Ghi chú. Chữ r được đặt trước tên tệp để ngăn các ký tự trong chuỗi tên tệp được coi là ký tự đặc biệt. Ví dụ: nếu có \temp trong địa chỉ tệp, thì \t được coi là ký tự tab và sẽ xảy ra lỗi do địa chỉ không hợp lệ. Chữ r làm cho chuỗi thô, nghĩa là nó báo rằng chuỗi không có bất kỳ ký tự đặc biệt nào. Có thể bỏ qua r nếu tệp nằm trong cùng thư mục và địa chỉ không được đặt. con trăn
File_object.write(str1)0 File_object.write(str1)1 File_object.write(str1)2 File_object.write(str1)3 File_object.write(str1)4 File_object.write(str1)5 File_object.write(str1)6 File_object.write(str1)7 File_object.write(str1)8 File_object.write(str1)9 File_object.writelines(L) for L = [str1, str2, str3]0 File_object.writelines(L) for L = [str1, str2, str3]1 File_object.write(str1)1 File_object.write(str1)2 File_object.writelines(L) for L = [str1, str2, str3]4_______11_______5 File_object.write(str1)5 File_object.writelines(L) for L = [str1, str2, str3]7 File_object.write(str1)7 Ở đây, file1 được tạo làm đối tượng cho MyFile1 và file2 làm đối tượng cho MyFile2 Đóng một tập tin close() đóng tệp và giải phóng không gian bộ nhớ mà tệp đó có được. Nó được sử dụng khi tệp không còn cần thiết hoặc nếu nó được mở ở một chế độ tệp khác. tệp_đối tượng. Thoát() con trănFile_object.writelines(L) for L = [str1, str2, str3]9 File_object.read([n])0 File_object.write(str1)0 File_object.write(str1)1 File_object.write(str1)2 File_object.write(str1)3 File_object.read([n])5 File_object.write(str1)5 File_object.write(str1)6 File_object.write(str1)7 File_object.read([n])9 Viết vào một tập tin Có hai cách để ghi vào một tập tin
File_object.write(str1)
File_object.writelines(L) for L = [str1, str2, str3] Đọc từ một tập tin Có ba cách để đọc dữ liệu từ một tệp văn bản
File_object.read([n])
File_object.readline([n])
File_object.readlines() Ghi chú. '\ n' được coi là một ký tự đặc biệt gồm hai byte Python3File_object.readline([n])0 File_object.readline([n])1 File_object.write(str1)0 File_object.write(str1)1 File_object.write(str1)2_______1_______3 File_object.readline([n])6 File_object.write(str1)5 File_object.readline([n])8 File_object.write(str1)7 File_object.readlines()0 File_object.write(str1)1 File_object.readlines()2 File_object.readlines()3_______1_______5 File_object.readlines()5 File_object.write(str1)5 File_object.readlines()7 File_object.readlines()8 File_object.write(str1)8 Output of Read function is Hello This is Delhi This is Paris This is London Output of Readline function is Hello Output of Read(9) function is Hello Th Output of Readline(9) function is Hello Output of Readlines function is ['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']0 Output of Read function is Hello This is Delhi This is Paris This is London Output of Readline function is Hello Output of Read(9) function is Hello Th Output of Readline(9) function is Hello Output of Readlines function is ['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']1_______56_______2 File_object.write(str1)7 Output of Read function is Hello This is Delhi This is Paris This is London Output of Readline function is Hello Output of Read(9) function is Hello Th Output of Readline(9) function is Hello Output of Readlines function is ['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']4 File_object.read([n])9_______56_______6 File_object.write(str1)8 File_object.write(str1)0 File_object.write(str1)1 File_object.write(str1)2 File_object.write(str1)3 File_object.readline([n])6 File_object.write(str1)5 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']4 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']5 File_object.write(str1)8 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______1_______3 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']9 File_object.write(str1)7 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______209_______2 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______209_______4 File_object.write(str1)8
Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']5 File_object.write(str1)8 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______1_______3_______210_______4 File_object.write(str1)7 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______210_______7 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______209_______4 File_object.write(str1)8
File_object.write(str1)7 File_object.write(str1)8 File_object.write(str1)05 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______1_______3 File_object.write(str1)08 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']5 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______1_______11 File_object.write(str1)12 File_object.write(str1)13 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______209_______4 File_object.write(str1)8
File_object.write(str1)7 File_object.write(str1)8 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______1_______3 File_object.write(str1)23 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']5 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______1_______26 File_object.write(str1)12 File_object.write(str1)13 File_object.write(str1)8
File_object.write(str1)7 File_object.write(str1)33 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______1_______3 File_object.write(str1)36 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']5 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______1_______39 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______209_______4 File_object.read([n])9 đầu ra Output of Read function is Hello This is Delhi This is Paris This is London Output of Readline function is Hello Output of Read(9) function is Hello Th Output of Readline(9) function is Hello Output of Readlines function is ['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n'] Thêm vào một tập tin Python3File_object.write(str1)43 File_object.write(str1)44 File_object.write(str1)0 File_object.write(str1)1 File_object.write(str1)2_______1_______3 File_object.readline([n])6 File_object.write(str1)5 File_object.readline([n])8 File_object.write(str1)7 File_object.readlines()0 File_object.write(str1)1 File_object.readlines()2 File_object.readlines()3_______1_______5 File_object.readlines()5 File_object.write(str1)5 File_object.readlines()7 File_object.readlines()8 Output of Read function is Hello This is Delhi This is Paris This is London Output of Readline function is Hello Output of Read(9) function is Hello Th Output of Readline(9) function is Hello Output of Readlines function is ['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']4 File_object.read([n])9 File_object.write(str1)8 File_object.write(str1)65 File_object.write(str1)0 File_object.write(str1)1 File_object.write(str1)2 File_object.write(str1)3 File_object.readline([n])6 File_object.write(str1)5 File_object.write(str1)6 File_object.write(str1)7 File_object.write(str1)74 Output of Read function is Hello This is Delhi This is Paris This is London Output of Readline function is Hello Output of Read(9) function is Hello Th Output of Readline(9) function is Hello Output of Readlines function is ['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']1_______1_______76 File_object.write(str1)7 File_object.read([n])9 File_object.write(str1)8 File_object.write(str1)0 File_object.write(str1)1 File_object.write(str1)2 File_object.write(str1)3 File_object.readline([n])6 File_object.write(str1)5 File_object.write(str1)86 File_object.write(str1)7 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______1_______3 File_object.write(str1)90 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']5 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______1_______93 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______209_______4 File_object.read([n])9 File_object.write(str1)8 File_object.write(str1)98 File_object.write(str1)0 File_object.write(str1)1 File_object.write(str1)2_______1_______3 File_object.readline([n])6 File_object.write(str1)5 File_object.readline([n])8 File_object.write(str1)7 File_object.writelines(L) for L = [str1, str2, str3]07 Output of Read function is Hello This is Delhi This is Paris This is London Output of Readline function is Hello Output of Read(9) function is Hello Th Output of Readline(9) function is Hello Output of Readlines function is ['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']1 File_object.writelines(L) for L = [str1, str2, str3]09 File_object.write(str1)7 File_object.read([n])9 File_object.write(str1)8 File_object.write(str1)0 File_object.write(str1)1 File_object.write(str1)2 File_object.write(str1)3 File_object.readline([n])6 File_object.write(str1)5 File_object.write(str1)86 File_object.write(str1)7 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______1_______3 File_object.writelines(L) for L = [str1, str2, str3]23 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']5 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______1_______93 Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']7_______209_______4 File_object.read([n])9 đầu ra Output of Readlines after appending ['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n'] Output of Readlines after writing ['Tomorrow \n']
Bài viết này được đóng góp bởi Harshit Agrawal. 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 bình luận nếu bạn thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề thảo luận ở trên Là một. py là một tệp văn bản?Tệp PY là tệp chương trình hoặc tập lệnh được viết bằng Python, một ngôn ngữ lập trình hướng đối tượng được giải thích . Nó có thể được tạo và chỉnh sửa bằng trình soạn thảo văn bản, nhưng yêu cầu trình thông dịch Python để chạy. Các tệp PY thường được sử dụng để lập trình máy chủ web và các hệ thống máy tính quản trị khác.
Bạn có thể tạo một tệp văn bản bằng Python không?Python cung cấp các chức năng sẵn có để tạo, ghi và đọc tệp . Có hai loại tệp có thể được xử lý trong python, tệp văn bản bình thường và tệp nhị phân (được viết bằng ngôn ngữ nhị phân, 0 và 1).
Làm cách nào để chuyển đổi txt sang tệp Python?Mở tệp TXT nguồn bằng Python. Gọi phương thức 'save()', chuyển tên tệp đầu ra có phần mở rộng DOC. Nhận kết quả chuyển đổi TXT dưới dạng DOC . |