Sự mô tả
Phương thức tệp Python Flush () Xóa bộ đệm bên trong, như FFLUSH của Stdio. Đây có thể là một không có trên một số đối tượng giống như tệp.flush() flushes the internal buffer, like stdio's fflush. This may be a no-op on some file-like objects.
Python tự động xả các tệp khi đóng chúng. Nhưng bạn có thể muốn xóa dữ liệu trước khi đóng bất kỳ tệp nào.
Cú pháp
Sau đây là Syntax cho phương thức Flush () -flush() method −
fileObject.flush();Thông số
Na
Giá trị trả về
Phương pháp này không trả về bất kỳ giá trị nào.
Thí dụ
Ví dụ sau đây cho thấy cách sử dụng phương thức Flush ().
#!/usr/bin/python # Open a file fo = open("foo.txt", "wb") print "Name of the file: ", fo.name # Here it does nothing, but you can call it with read operation. fo.flush() # Close opend file fo.close()Khi chúng tôi chạy trên chương trình, nó tạo ra kết quả sau -
Name of the file: foo.txtpython_files_io.htm
Trong Python 3, print() hiện là một hàm và sử dụng các đối số để kiểm soát đầu ra của nó. Trong bài học này, bạn sẽ tìm hiểu về các đối số sep, end và flush.print() is now a function and uses arguments to control its output. In this lesson, you’ll learn about the sep, end, and flush arguments.
Theo mặc định, print() chèn một khoảng trống giữa các mục mà nó đang in. Bạn có thể thay đổi điều này bằng cách sử dụng tham số sep:
>>>
>>> print('There are', 6, 'members of Monty Python') There are 6 members of Monty Python >>> message = 'There are' + 6 + 'members of Monty Python' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate str (not "int") to str >>> message = 'There are' + str(6) + 'members of Monty Python' >>> print(message) There are6members of Monty Python >>> print('There are', 6, 'members of Monty Python', sep='😀') There are😀6😀members of Monty Python >>> print('There are', 6, 'members of Monty Python', sep=' ') There are 6 members of Monty Python >>> print('There are', 6, 'members of Monty Python', sep=None) There are 6 members of Monty Python >>> print('There are', 6, 'members of Monty Python', sep='') There are6members of Monty Python >>> print('There are', 6, 'members of Monty Python', sep='\n') There are 6 members of Monty Python >>> data = [ ... ['year', 'last', 'first'], ... [1943, 'Idle', 'Eric'], ... [1939, 'Cleese', 'John'] ... ] >>> for row in data: ... print(*row, sep=',') ... year,last,first 1943,Idle,Eric 1939,Cleese,JohnTrừ khi được nói khác, print() thêm #!/usr/bin/python # Open a file fo = open("foo.txt", "wb") print "Name of the file: ", fo.name # Here it does nothing, but you can call it with read operation. fo.flush() # Close opend file fo.close() 2 vào cuối những gì đang được in. Điều này có thể được thay đổi với tham số end. Đầu ra từ print() đi vào một bộ đệm. Khi bạn thay đổi tham số end, bộ đệm không còn bị xóa. Để đảm bảo rằng bạn nhận được đầu ra ngay khi print() được gọi, bạn cũng cần sử dụng tham số #!/usr/bin/python # Open a file fo = open("foo.txt", "wb") print "Name of the file: ", fo.name # Here it does nothing, but you can call it with read operation. fo.flush() # Close opend file fo.close() 7:
import time def count_items(items): print('Counting ', end='', flush=True) num = 0 for item in items: num += 1 time.sleep(1) print('.', end='', flush=True) print(f'\nThere were {num} items')
Bạn có thể kết hợp sep và end để tạo danh sách, đầu ra CSV, danh sách viên đạn và hơn thế nữa.