Góp ý (bổ sung) bài dạy python số 15 của anh Đạt trên Youtube!

@ltd
Trong bài python số 15 chỗ cái lệnh “print txt.read()” em phát hiện ra 1 tác dụng nữa của câu lệnh read(). Là khi ta thêm 1 số vào trong ngoặc, thì số đó sẽ giới hạn số ký tự được in ra của file mà chúng ta sử dụng lệnh read().
VD : nội dung của biến “txt” là : “This is my dog.”. Nếu ta sử dụng lệnh “print txt.read(1)” thì kết quả được in ra sau khi sử dụng lệnh đó là : “T”
-Mọi người có thấy khó hiểu không, nếu có hãy để lại comment :wink:

7 Likes

Đúng rồi cái này cũng cần nói tới nữa :slight_smile:

To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string (in text mode) or bytes object (in binary mode). size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, f.read() will return an empty string (’’).

1 Like

Thật mừng vì không cần xem video sau của anh em vẫn biết được cái mánh đó. Yeah !!!:grin:

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?