Python OS文件/目录 os.fdatasync() 方法

Python OS文件/目录 os.fdatasync() 方法

描述

fdatasync() 方法强制将具有文件描述符fd的文件写入磁盘。这不会强制更新元数据。如果您想刷新缓冲区,则可以使用此方法。

语法

以下是fdatasync()方法的语法 –

os.fdatasync(fd)

参数

  • fd − 这是要写入数据的文件描述符。

返回值

此方法不返回任何值。

示例

以下示例显示了fdatasync()方法的用法−

import os, sys

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

# Write one string
line="this is test"

# string needs to be converted byte object
b=str.encode(line)
os.write(fd, b)

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

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

# Close opened file
os.close( fd )
print ("Closed the file successfully!!")

当我们运行上述程序时,它会产生以下结果 –

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程