Skip to main content

Command Palette

Search for a command to run...

Write() in python

Updated
1 min readView as Markdown
Write() in python
X

"I am currently a Software Engineering student at ALX. I'm passionate about technology and enjoy conducting research to find answers on my own. I have a natural inclination to ask 'WHY' more often than 'HOW'.

"While working on projects at ALX, I have acquired a wealth of interesting and diverse knowledge about software engineering and computer science in general. Therefore, I needed a place to store and save all this information, allowing me to refer back to it whenever I forget."

we learnt previously in c that write() takes 3 arguments

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
  • fd is the file descriptor of the file or socket where the data will be written.

  • buf is a pointer to the buffer containing the data to be written.

  • count is the number of bytes to be written from the buffer.

that's not the case in python
in python write takes just two arguments and this time we don't include <unistd.h> we import the os module(operating system).

import os
os.write(fd, data)

The os.write() function takes two arguments:

  1. fd (file descriptor): It represents the file descriptor where the data will be written. In the case of standard output, it is typically represented by the value 1.

  2. data (bytes-like object): It is the data that you want to write to the file descriptor. It should be a bytes-like object, such as a byte string (b"...") or a bytes object (bytes(...)).

So, you need to provide both the file descriptor and the data to be written when calling the os.write() function.

More from this blog

PERSONAL BLOG

110 posts

Use the search button to search for a specific topic or keyword *all posts are updated on the go*