os.write and file descriptor

Grant Edwards grante at visi.com
Tue Feb 4 17:15:12 EST 2003


In article <mailman.1044394889.16825.python-list at python.org>, Inyeol Lee wrote:

> I'm writing a program which writes to file descriptor 3.
> I've tried os.write() but it generates exception;
> 
>>>> import os
>>>> os.write(3, "hello")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> OSError: [Errno 9] Bad file number

What happens if you use 1 or 2 instead of 3?

Why do you think that 3 should be valid?

> I've also tried equivalent C code, and it works fine;

It doesn't for me:

$cat testit.c
#include <stdio.h>
#include <unistd.h>
#include <string>

int main(void)
{
  int n;
  char s[] = "hello, world\n";
  n = write(3, s, strlen(s));
  if (n != strlen(s))
    perror("write");
  return 0;
}


$ ./testit
write: Bad file descriptor

> Is the usage of os.write() different from C? Am I missing
> something?

No.

-- 
Grant Edwards                   grante             Yow!  World War Three can
                                  at               be averted by adherence
                               visi.com            to a strictly enforced
                                                   dress code!




More information about the Python-list mailing list