當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python 3 os.fsync()用法及代碼示例


描述

方法fsync()強製將文件描述符為 fd 的文件寫入磁盤。如果您從 Python 文件對象 f 開始,首先執行 f.flush(),然後執行 os.fsync(f.fileno()),以確保與 f 關聯的所有內部緩衝區都寫入磁盤。

用法

以下是語法fsync()方法 -

os.fsync(fd)

參數

fd- 這是緩衝區同步的文件描述符是必需的。

返回值

此方法不返回任何值。

示例

下麵的例子展示了 fsync() 方法的用法。

#!/usr/bin/python3
import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string
line = "this is test"
b = line.encode()
os.write(fd, b)

# Now you can use fsync() method.
# Infact here you would not be able to see its effect.
os.fsync(fd)

# Now read this file from the beginning
os.lseek(fd, 0, 0)
line = os.read(fd, 100)
b = line.decode()
print ("Read String is:", b)

# Close opened file
os.close( fd )

print ("Closed the file successfully!!")

結果

當我們運行上述程序時,它會產生以下結果 -

Read String is: this is test
Closed the file successfully!!

相關用法


注:本文由純淨天空篩選整理自 Python 3 - os.fsync() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。