Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。
os.sendfile()
Python中的方法用於從指定的偏移量開始,將指定數量的字節從指定的源文件描述符複製到指定的dest文件描述符。此方法返回發送的字節總數,如果達到EOF(文件末尾),則返回0。
用法: os.sendfile(dest, source, offset, count)
參數:
dest:代表目標文件的文件描述符。
source:代表源文件的文件描述符
offset:代表起始位置的整數值。從該位置開始計算要發送的字節數。
count:一個整數值,表示要從源文件描述符發送的字節總數。
返回類型:此方法返回一個整數值,該整數值表示從源文件描述符發送到目標文件描述符的字節總數。如果達到EOF,則返回0。
將以下文本視為名為“ Python_intro.txt”的文件的內容。
Python is a widely-used general-purpose, high-level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code. Python is a programming language that lets you work quickly and integrate systems more efficiently.
os.sendfile()
方法# Python program to explain os.sendfile() method
# importing os module
import os
# Source file path
source = './Python_intro.txt'
# destination file path
dest = './newfile.txt'
# Open both files and get
# the file descriptor
# using os.open() method
src_fd = os.open(source, os.O_RDONLY)
dest_fd = os.open(dest, os.O_RDWR | os.O_CREAT)
# Now send n bytes from
# source file descriptor
# to destination file descriptor
# using os.sendfile() method
offset = 0
count = 100
bytesSent = os.sendfile(dest_fd, src_fd, offset, count)
print("% d bytes sent / copied successfully." % bytesSent)
# Now read the sent / copied
# content from destination
# file descriptor
os.lseek(dest_fd, 0, 0)
str = os.read(dest_fd, bytesSent)
# Print read bytes
print(str)
# Close the file descriptors
os.close(src_fd)
os.close(dest_fd)
100 bytes sent/copied successfully. b'Python is a widely used general-purpose, high level programming language. It was initially designed '
參考: https://docs.python.org/3/library/os.html#os.sendfile
相關用法
- Python next()用法及代碼示例
- Python os.dup()用法及代碼示例
- Python set()用法及代碼示例
- Python Decimal max()用法及代碼示例
- Python PIL ImageOps.fit()用法及代碼示例
- Python os.rmdir()用法及代碼示例
- Python sympy.det()用法及代碼示例
- Python Decimal min()用法及代碼示例
- Python os.readlink()用法及代碼示例
- Python os.writev()用法及代碼示例
- Python os.readv()用法及代碼示例
- Python PIL RankFilter()用法及代碼示例
- Python os.rename()用法及代碼示例
- Python os.makedev()用法及代碼示例
注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.sendfile() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。