Dùng hàm fdatasync(fd) ép ghi file với filedescriptor fd ra disk. Không ép update metadata. Chỉ dùng trên Unix.
Giải pháp cross-platform hơn là fsync(fd) cũng ép ghi file ra disk. Trên Unix gọi native fsync(); Windows gọi _commit().
Ví dụ
import os, sys
# Mở file
fd = os.open("foo.txt", os.O_RDWR | os.O_CREAT)
os.write(fd, "This is test")
# Dùng fsync()
os.fsync(fd)
# Đọc file từ đầu
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print("Read String is : ", str)
os.close(fd)
Kết quả
Chạy chương trình cho kết quả:
Read String is : This is test