Trong loạt bài hướng dẫn Python dành cho người mới bắt đầu, chúng ta đã tìm hiểu thêm về các hàm chuỗi Python trong bài hướng dẫn trước Show Python cung cấp một tính năng quan trọng để đọc dữ liệu từ tệp và ghi dữ liệu vào tệp Trong hầu hết các ngôn ngữ lập trình, tất cả các giá trị hoặc dữ liệu được lưu trữ trong một biến vốn có khả năng thay đổi. Bởi vì dữ liệu sẽ chỉ được lưu trữ trong các biến đó trong thời gian chạy và sẽ bị mất khi kết thúc chương trình. Do đó, tốt hơn là lưu những dữ liệu này vĩnh viễn bằng cách sử dụng tệp Python xử lý các tệp như vậy?Nếu bạn đang làm việc trong một ứng dụng phần mềm lớn, nơi bạn xử lý một lượng lớn dữ liệu, thì bạn không thể mong đợi dữ liệu đó được lưu trữ trong một biến vì về bản chất, các biến này rất dễ bị hài. Do đó, khi bạn sẵn sàng xử lý các tình huống như vậy, vị trí của tệp sẽ xuất hiện trong hình Vì các tệp không dễ bay hơi về bản chất, dữ liệu sẽ được lưu trữ vĩnh viễn trên một thiết bị thứ cấp như ổ cứng và bằng cách sử dụng python, chúng tôi sẽ xử lý các tệp này trong các ứng dụng của mình Bạn đang nghĩ về cách python sẽ xử lý các tệp? Hội đội đến ví dụ về cách người bình hội chủ của lại với. Nếu muốn đọc dữ liệu từ tệp hoặc ghi dữ liệu vào tệp, trước tiên chúng ta sẽ mở tệp hoặc tạo tệp mới nếu tệp chưa tồn tại, sau đó thực hiện các thao tác đọc/ghi thông thường. , Lưu tệp và đóng lại Tương tự, chúng ta có thể thực hiện các thao tác tương tự trong python bằng cách sử dụng một số phương thức hoặc hàm tích hợp sẵn. Các loại tệp PythonCó hai loại tệp Python và mỗi loại được giải thích chi tiết bên dưới với các ví dụ để bạn dễ hiểu họ đang
Một tệp trong PythonHầu hết các tệp mà người ta định nghĩa trong hệ thống máy tính cho đến nay đều đã được xác định Thí dụ
Đọc thêm. 7z Tất cả các tệp theo một định dạng cụ thể. Chúng tôi có thể mở một số tệp nhị phân trong trình soạn thảo văn bản tiêu chuẩn, nhưng chúng tôi không thể đọc nội dung của tệp. Đó là bởi vì tất cả các tệp nhị phân sẽ được mã hóa ở định dạng nhị phân mà chỉ máy tính hoặc máy tính mới có thể hiểu được Để xử lý các tệp như thế này, chúng tôi cần một loại phần mềm cụ thể để mở tệp đó Ví dụ. Bạn cần có phần mềm Microsoft word để mở file. tài liệu. Tương tự, bạn cần một phần mềm đọc pdf để mở các tập tin. pdf và bạn cần một phần mềm chỉnh sửa ảnh để đọc file ảnh, v. v Tệp văn bản trong PythonCác tệp văn bản không chứa bất kỳ mã cụ thể nào và có thể được mở trong trình soạn thảo văn bản tiêu chuẩn. Thí dụ
Trong hướng dẫn này, chúng ta sẽ xem cách xử lý cả tệp văn bản và tệp nhị phân với một số ví dụ cổ điển Nó xử lý các tệp PythonĐiều quan trọng nhất là có 4 loại thao tác mà Python có thể xử lý trên tệp các hoạt động khác bao gồm Python Tạo và mở tệpPython có một hàm gọi là open() để mở tệp Nó cần ít nhất một đối số như được đề cập dưới đây. Phương thức mở trả về một đối tượng tệp được sử dụng để truy cập ghi, đọc và các phương thức tích hợp khác cúpáp file_object = open (tên_tệp, chế độ) Ở đây file_name là tên file hoặc vị trí file muốn mở và file_name cũng phải có phần mở rộng file. There is in the test. txt - ក្រង្រ្រ្រ tra là tên ក្រ្រ្រ. phần mở rộng tệp txt Chế độ trong trận đấu chức năng một mạng lưới cho Python thay đổi đến khi đến nơi hội nhập để thoát thết tấn tệp
Lưu ý. Các chế độ này chỉ được sử dụng để mở, đọc hoặc ghi tệp văn bản Khi sử dụng tệp thứ hai, bạn phải sử dụng các cài đặt giống như ký tự 'b' ở cuối. Để Python có thể hiểu rằng chúng ta đang tương tác với các tệp nhị phân
ví dụ 1 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)46 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)2 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)3 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4 Trong ví dụ trên, nười đội được đặt tên là 'bài kiểm tra. txt' has attị trí 'C. /Documents /Python /' and people are going to Đội đến điện của-ghi chủ một người linh điều chỉnh ví dụ 2 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)46 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.remove (tên_tệp)4 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)3 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4 Trong ví dụ trên, nười đội được đặt tên là 'img. bmp' has Tune position “C. /Documents /Python/”, But here who are the start-up Python Đọc từ tệpĐể đọc một tệp trong python, bạn cần đọc tệp Có ba cách díc nún ta có thêm đọc trong file python
Ở đây, n là số byte được đọc Đầu tiên chúng ta tạo một file văn bản mẫu như hình bên dưới Bây giờ bạn có thể đọc những gì mỗi phương pháp đọc ví dụ 1 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)466 Lần đầu tiên Xin chào Ở đây chúng tôi đang mở một tệp thử nghiệm. txt là chỉ đọc và chỉ đọc 5 ký tự đầu tiên của tệp bằng phương thức my_file. đọc (5) Lần đầu tiên ví dụ 2 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)466 Lần đầu tiên Xin chào 三宝 Here nười của nội giám đốc số nău bên trong quá trình đọc (). Vì vậy, nó sẽ không đọc tất cả nội dung trong tệp Lần đầu tiên ví dụ 3 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)466 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)488 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)489 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)490 Lần đầu tiên anh ta Trang này trả về 2 từ đầu tiên của dòng tiếp theo Lần đầu tiên Ví dụ 4 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)466 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)502 Lần đầu tiên Chào thế giới Sử dụng chức năng này, chúng ta có thể đọc nội dung của tệp theo từng dòng Lần đầu tiên Ví dụ 5 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)466 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)514 Lần đầu tiên ['Xin chào thế giới\n', 'Xin chào Python\n', 'Chào buổi sáng'] Ở đây chúng tôi đang đọc tất cả các dòng của văn bản bao gồm cả văn bản mới Lần đầu tiên Bây giờ chúng ta hãy xem xét kỹ hơn việc đọc một tệp Đọc một dòng cụ thể từ một tập tin os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)46 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)504 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)505 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)506 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)507 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)00 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)01 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)02 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)02 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)3 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)06 Lần đầu tiên Bạn có ổn không? Trong ví dụ trên, nười đội động đến của tệp công thức thứ 4 'kiểm tra'. txt' you way trarant " loop la for" Lần đầu tiên Đọc toàn bộ tập tin cùng một lúc os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)07 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)09_______150 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50____10 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)15 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)16 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)19 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)20 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)22 Lần đầu tiên Xin chào thảo quản Lần đầu tiên Python Ghi vào tập tinĐể ghi dữ liệu vào tệp phải mở tệp ở chế độ ghi Ta cần hết sức cẩn thận khi ghi dữ liệu vào tệp vì nó sẽ ghi đè lên nội dung tệp đang ghi và toàn bộ dữ liệu trước đó sẽ bị xóa Có các cách ghi dữ liệu vào file như hình dưới
ví dụ 1 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)32 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)33 Đoạn mã trên ghi chuỗi 'Hello World' và filha 'test. txt' Trước khi ghi dữ liệu vào file test. txt Lần đầu tiên ví dụ 2 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)32 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)44 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)45 Dòng đầu tiên sẽ là 'Xin chào thế giới' và như chúng tôi đã đề cập đến ký tự \n, nó sẽ chuyển sang dòng tiếp theo của tệp và sau đó viết 'Xin chào Python' Hãy nhớ rằng, nếu bạn không có mật khẩu, thì dữ liệu sẽ được ghi liên tục trong tệp văn bản như 'Xin chào WorldHelloPython' Lần đầu tiên ví dụ 3 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)46____147 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)32 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)59 Đoạn mã trên ghi danh sách dữ liệu vào kiểm tra filha '. txt cùng một lúc Lần đầu tiên Đăng nhập tệp PythonĐể nối thêm dữ liệu vào một tệp, bạn phải mở tệp ở chế độ 'a +' để bạn có quyền truy cập vào cả hai chế độ nối thêm và ghi ví dụ 1 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)69 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)3 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)72 Đoạn mã trên vùng đất của 'Apple' phản đối cuối tập 'test. txt' Lần đầu tiên ví dụ 2 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)69 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)3 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)85 Đoạn mã trên Đạn Đạn 'Apple' đến 'test'. txt' trong một dòng mới Lần đầu tiên ví dụ 3 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)46____147 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)88 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)69 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)3 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)59 Code on connection list data into file 'test. txt' Lần đầu tiên Ví dụ 4 os.remove (tên_tệp)02 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.remove (tên_tệp)04 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.remove (tên_tệp)08 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.remove (tên_tệp)14 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.remove (tên_tệp)16 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)3 os.remove (tên_tệp)18 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.remove (tên_tệp)20 os.remove (tên_tệp)21 os.remove (tên_tệp)22 os.remove (tên_tệp)23 os.remove (tên_tệp)24 Trong đoạn thứ ba, chúng tôi đang thêm danh sách dữ liệu vào tệp 'kiểm tra'. txt'. Ở đây, bạn có thể thấy rằng chúng ta đã sử dụng phương thức tell() để kiểm tra trạng thái hiện tại của con trỏ tìm kiếm (bù trừ). offset kan you you at the number is 0,1 and 2. Lần đầu tiên Python Đóng tệpĐể đóng tệp, trước tiên hãy mở tệp trước. Trong python, chúng ta có thể gọi close() để đóng file Bất cứ khi nào bạn mở một tệp, điều quan trọng là phải đóng nó lại, đặc biệt nếu nó không hoạt động. Bởi vì nếu chúng ta không gọi phương thức close hoặc write, mọi dữ liệu chúng ta ghi vào tệp sẽ không được lưu vào tệp ví dụ 1 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)466 ví dụ 2 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)32 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)33 Thay đổi hoặc xóa tệp trong PythonPython cung cấp cho chúng ta mô-đun “os” với một số phương thức tích hợp sẵn sẽ giúp chúng ta thực hiện các thao tác với tệp như đổi tên và xóa tệp Để sử dụng mô-đun này, trước hết, chúng ta cần nhập mô-đun “os” vào chương trình của mình rồi gọi các phương thức liên quan đổi tên () rename method () nội hội phống hai đồng số tại là tên đồng đến động và tên đồng khối cúpáp os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới) ví dụ 1 os.remove (tên_tệp)46 đây 'kiểm tra. txt' và 'test1. txt' is a new section name Bạn có thể chỉ định vị trí của mình cũng như được hiển thị trong ví dụ bên dưới ví dụ 2 os.remove (tên_tệp)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.remove (tên_tệp)53 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.remove (tên_tệp)59 ក្រ khi dịch tên đồng Vì vậy, khi bạn chạy chương trình gỡ bỏ () Bạn có thể sử dụng phương thức remove() để xóa một tập tin bằng cách cung cấp tên tập tin hoặc vị trí tập tin mà bạn muốn xóa. cúpáp os.remove (tên_tệp) ví dụ 1 Đây là một bài kiểm tra. txt' mà bạn muốn xóa Tương tự, chúng ta có thể di chuyển qua các tệp cũng như các đối số như trong ví dụ bên dưới ví dụ 2 os.remove (tên_tệp)60 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.remove (tên_tệp)66 Mã hóa trong tệpMã hóa tệp đại diện cho việc chuyển đổi các ký tự thành một định dạng cụ thể mà chỉ máy mới có thể hiểu được Các máy khác nhau có các định dạng mã khác nhau như hình bên dưới
Hãy xem cách nó hoạt động với một số ví dụ ví dụ 1 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.remove (tên_tệp)76 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.remove (tên_tệp)78 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.remove (tên_tệp)80 os.remove (tên_tệp)81 os.remove (tên_tệp)82 os.remove (tên_tệp)23 os.remove (tên_tệp)84 Lần đầu tiên Định dạng mã mặc định của Microsoft Windows là cp1252 Ở đây, tôi đã chạy chương trình của mình trên Windows, vì vậy nó được đặt thành 'cp1252' Lần đầu tiên Chúng tôi cũng có thể thay đổi định dạng mã hóa của tệp bằng cách mở tệp ví dụ 2 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.remove (tên_tệp)76 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.remove (tên_tệp)96 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.remove (tên_tệp)98 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4600 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4601 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4602 os.remove (tên_tệp)81 os.remove (tên_tệp)23 os.remove (tên_tệp)84 Lần đầu tiên Định dạng tệp là. cp437 Lần đầu tiên ví dụ 3 os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.remove (tên_tệp)76 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.remove (tên_tệp)96 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4619 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4620 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4621 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4600 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4601 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4602 os.remove (tên_tệp)81 os.remove (tên_tệp)23 os.remove (tên_tệp)84 Lần đầu tiên Định dạng tệp là. utf-16 Lần đầu tiên Viết và đọc dữ liệu từ một tập tinCác tệp nhị phân lưu trữ dữ liệu ở định dạng nhị phân (số 0 và số 1) mà máy có thể hiểu được. Do đó, khi chúng tôi mở một tệp nhị phân trên máy của mình, nó sẽ giải mã dữ liệu và hiển thị ở định dạng mà con người có thể đọc được Thí dụ # Tạo một tập tin os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4639 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4640 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4641 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)3 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4644 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4646 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4647 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4649 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4650 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4652 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4654 Trong ví dụ trên, cái đầu tiên là tạo tệp bfile. bin' với quyền đọc và ghi và bất kỳ dữ liệu nào bạn muốn nhập vào tệp phải được mã hóa trước khi bạn gọi phương thức ghi Ngoài ra, chúng tôi đang ở trong dữ liệu mà không cần giải mã, vì vậy chúng tôi có thể thấy chính xác dữ liệu trông như thế nào bên trong tệp khi nó được mã hóa và chúng tôi cũng ở trong cùng một dữ liệu khi giải quyết nó để mọi người có thể đọc được Lần đầu tiên Dữ liệu nhị phân. b'Xin chào Python ' Lần đầu tiên Thuộc tính I/O của tệpThuộc tính Sự kiện TênRepay đến tên đến Cáchếế Chế độ đi đến đến Trình họaRetả đến đến hợa Đã đóng Trả về true nếu tệp bị đóng ngược lại trả về false Thí dụ os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)69 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)3 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4668 os.remove (tên_tệp)23 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4670 os.remove (tên_tệp)21 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4672 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4668 os.remove (tên_tệp)23 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4670 os.remove (tên_tệp)21 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4678 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4668 os.remove (tên_tệp)23 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4682 os.remove (tên_tệp)81 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4684 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4686 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4601 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4688 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4686 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4601 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4688 Lần đầu tiên Name file is what? . /Documents/Python/test. txt Lần đầu tiên Hãy thử một số phương pháp tập tin khác Thí dụ os.remove (tên_tệp)7 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)47 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)48 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)49 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)51 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)0 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)50 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4702 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)3 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4705 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4686 os.remove (tên_tệp)21 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4709 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4686 os.remove (tên_tệp)21 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4713 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)487 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4600 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4601 os.rename (tên_tệp_ hiện_tại, tên_tệp_tính mới)4717 Lần đầu tiên Xin chào Python Lần đầu tiên Định dạng tệp PythonHàm sốGiải trị mở()Đọ mở một tệp gần()Đó là một tệp đang mởthư phưong số()Retả nhẩm một nộm đang lắng đọng để đọc (n)Có thể đọc n ký tự 'n' từ tệp đến cuối tệp () Quay lại true nếu tệp có thể đọc readline ()Đọc và trả về một dòng từ tệp đã đọc ()Đọc và trả về tất cả các dòng từ tìm kiếm tệp ()Trả về true nếu tệp hỗ trợ truy cập ngẫu nhiên()Trả về ba tệp hiện có ( )Trả về true nếu tệp có thể được ghi()Trả về một chuỗi dữ liệu cho filewritelines ()Khôi phục dữ liệu vào tệp |