Change language

Python | os.sendfile () method

|

os.sendfile() in Python is used to copy a specified number of bytes from a specified source file descriptor to the specified file descriptor dest, starting at the specified offset. 
This method returns the total number of bytes sent, and if EOF (end of file) is reached, it returns 0.

Syntax: os.sendfile (dest, source, offset, count)

Parameters:
dest: A file descriptor representing destination file.
source: A file descriptor representing source file
offset: An integer value representing starting position. Bytes to be sent will be counted from this position.
count: An integer value denoting total number of bytes to be sent from source file descriptor.

Return Type: This method returns an integer value which represents the total of bytes sent from source file descriptor to dest file descriptor. 0 is returned if EOF is reached.

Consider the text below as the contents of a file named & # 39; Python_intro.txt & # 39; .

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.

Code: using the os.sendfile method ()

# Python program to explain the os.sendfile () method

 
# import of the os module

import os

  

 
# Path to source file

source = ’. / Python_intro.txt’

  
# path to destination file

dest = ’. / newfile.txt’

  
# Open both files and get
# file descriptor
# using the 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 the 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 submitted / copied
# content from destination
# file descriptor

os.lseek (dest_fd, 0 , 0 )

str = os.read (dest_fd, bytesSent)

 
# Print bytes read

print ( str )

 
# Close file descriptors
os.close (src_fd)
os .close (dest_fd)

Exit:

 100 bytes sent / copied success fully. b’Python is a widely used general-purpose, high level programming language. It was initially designed ’

Link: https://docs.python.org/3/library/os.html#os.sendfile

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically