title: Bad file descriptor on a python script but the output is correct
body: <p>The output of the script below is correct, but everytime it prints a line it prints the error too.</p>
  <pre><code>import os
  
  for pid in os.listdir ( '/proc' ) :
      if pid.isdigit ( ) :
          try :
              p = open ( os.path.join ( '/proc' , pid , 'cmdline' ) , 'r' )
              CMD = p.read ( )
              try:
                  tty = os.ttyname ( int ( pid ) )
              except OSError as error:
                  print(error)
              print ( int ( pid ) , CMD , tty )
              p.close()
          except OSError as error:
              continue
  </code></pre>
  <p>output:</p>
  <pre><code>(1, '/sbin/init\x00splash\x00', '/dev/pts/0')
  (2, '', '/dev/pts/0')
  [Errno 25] Inappropriate ioctl for device
  (3, '', '/dev/pts/0')
  [Errno 9] Bad file descriptor
  (4, '', '/dev/pts/0')
  [Errno 9] Bad file descriptor
  
  etc....
  </code></pre>