The Wayback Machine - https://web.archive.org/web/20240609103645/https://www.geeksforgeeks.org/python-os-write-method/
Open In App

Python | os.write() method

Last Updated : 18 Jun, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

os.write() method in Python is used to write a bytestring to the given file descriptor.

A file descriptor is small integer value that corresponds to a file that has been opened by the current process. It is used to perform various lower level I/O operations like read, write, send etc.

Note: os.write() method is intended for low-level operation and should be applied to a file descriptor as returned by os.open() or os.pipe() method.

Syntax: os.write(fd, str)

Parameter:
fd: The file descriptor representing the target file.
str: A bytes-like object to be written in the file.

Return Type: This method returns an integer value which represents the number of bytes actually written.

Code: Use of os.write() method to write a bytestring to a given file descriptor




# Python program to explain os.write() method 
    
# importing os module 
import os
  
# File path 
path = "/home / ihritik / Documents / GeeksForGeeks.txt"
  
  
# Open the file and get
# the file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_RDWR)
  
  
# String to be written
s = "GeeksForGeeks: A Computer science portal for Geeks."
  
# Convert the string to bytes
line = str.encode(s)
  
# Write the bytestring to the file
# associated with the file
# descriptor fd and get the number of
# Bytes actually written
numBytes = os.write(fd, line)
  
print("Number of bytes written:", numBytes)
  
# close the file descriptor
os.close(fd)


Output:

Number of bytes written: 51

Previous Article
Next Article

Similar Reads

Pgmagick write() method - Python
The write() function is an inbuilt function in the Pgmagick library which is used to write an intermediate image. The current image is written to the specified filename and then processing continues using that image. Syntax: write(filename) Parameters: This function accepts single parameter which is mentioned above and defined below: filename: This
1 min read
Why we write #!/usr/bin/env python on the first line of a Python script?
The shebang line or hashbang line is recognized as the line #!/usr/bin/env python. This helps to point out the location of the interpreter intended for executing a script, especially in Unix-like operating systems. For example, Linux and macOS are Unix-like operating systems whose executable files normally start with a shebang followed by a path to
2 min read
How to write an empty function in Python - pass statement?
In C/C++ and Java, we can write empty function as following // An empty function in C/C++/Java void fun() { } In Python, if we write something like following in Python, it would produce compiler error. # Incorrect empty function in Python def fun(): Output : IndentationError: expected an indented block In Python, to write empty functions, we use pa
1 min read
Python - Write Bytes to File
Files are used in order to store data permanently. File handling is performing various operations (read, write, delete, update, etc.) on these files. In Python, file handling process takes place in the following steps: Open filePerform operationClose file There are four basic modes in which a file can be opened― read, write, append, and exclusive c
2 min read
Python | Write multiple files data to master file
Given a number of input files in a source directory, write a Python program to read data from all the files and write it to a single master file. Source directory contains n number of files, and structure is same for all files. The objective of this code is to read all the files one by one and then append the output into a single master file having
2 min read
Python | Create and write on excel file using xlsxwriter module
XlsxWriter is a Python module for writing files in the XLSX file format. It can be used to write text, numbers, and formulas to multiple worksheets. Also, it supports features such as formatting, images, charts, page setup, auto filters, conditional formatting and many others.Use this command to install xlsxwriter module: pip install xlsxwriter Not
3 min read
Read, Write and Parse JSON using Python
JSON is a lightweight data format for data interchange that can be easily read and written by humans, and easily parsed and generated by machines. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called JSON. Example of JSON String s = '{"id":01, "name": "Emily", "language": ["C++", "Python"]}
4 min read
Python OpenCv: Write text on video
OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. cv2.putText() method inserts a text on the
2 min read
Python - Write "GFG" using Turtle Graphics
IN this article we will learn how to write "GFG" using Turtle Graphics in Python. For that lets first know what is Turtle Graphics. Turtle graphicsbackward(length): moves the pen in the backward direction by x unit.right(angle): rotate the pen in the clockwise direction by an angle x.left(angle): rotate the pen in the anticlockwise direction by an
2 min read
Read and Write TOML Files Using Python
TOML file stand for (Tom's Obvious, Minimum Language). The configuration files can be stored in TOML files, which have the .toml extension. Due to its straightforward semantics, which strives to be "minimal," it is supposed to be simple to read and write. It is also made to clearly map to a dictionary. The TOML's syntax primarily consists of key =
6 min read
Article Tags :
Practice Tags :