@@ -534,86 +534,6 @@ file object, the same type returned by the built-in :func:`open` function.
534
534
Thus, to read *n* bytes from a pipe *p* created with :func:` os.popen` , you need to
535
535
use ` ` p.read(n)` ` .
536
536
537
-
538
- .. XXX update to use subprocess. See the :ref:` subprocess-replacements` section.
539
-
540
- How do I run a subprocess with pipes connected to both input and output?
541
- ------------------------------------------------------------------------
542
-
543
- Use the :mod:` popen2` module. For example::
544
-
545
- import popen2
546
- fromchild, tochild = popen2.popen2(" command" )
547
- tochild.write(" input\n " )
548
- tochild.flush()
549
- output = fromchild.readline()
550
-
551
- Warning: in general it is unwise to do this because you can easily cause a
552
- deadlock where your process is blocked waiting for output from the child
553
- while the child is blocked waiting for input from you. This can be caused
554
- by the parent expecting the child to output more text than it does or
555
- by data being stuck in stdio buffers due to lack of flushing.
556
- The Python parent can of course explicitly flush the data it sends to the
557
- child before it reads any output, but if the child is a naive C program it
558
- may have been written to never explicitly flush its output, even if it is
559
- interactive, since flushing is normally automatic.
560
-
561
- Note that a deadlock is also possible if you use :func:` popen3` to read
562
- stdout and stderr. If one of the two is too large for the internal buffer
563
- (increasing the buffer size does not help) and you ` ` read()` ` the other one
564
- first, there is a deadlock, too.
565
-
566
- Note on a bug in popen2: unless your program calls ` ` wait()` ` or
567
- ` ` waitpid()` ` , finished child processes are never removed, and eventually
568
- calls to popen2 will fail because of a limit on the number of child
569
- processes. Calling :func:` os.waitpid` with the :const:` os.WNOHANG` option can
570
- prevent this; a good place to insert such a call would be before calling
571
- ` ` popen2` ` again.
572
-
573
- In many cases, all you really need is to run some data through a command and
574
- get the result back. Unless the amount of data is very large, the easiest
575
- way to do this is to write it to a temporary file and run the command with
576
- that temporary file as input. The standard module :mod:` tempfile` exports a
577
- :func:` ~tempfile.mktemp` function to generate unique temporary file names. ::
578
-
579
- import tempfile
580
- import os
581
-
582
- class Popen3:
583
- " " "
584
- This is a deadlock-safe version of popen that returns
585
- an object with errorlevel, out (a string) and err (a string).
586
- (capturestderr may not work under windows.)
587
- Example: print(Popen3(' grep spam' ,' \n\nhere spam\n\n' ).out)
588
- " " "
589
- def __init__(self,command,input=None,capturestderr=None):
590
- outfile=tempfile.mktemp()
591
- command=" ( %s ) > %s" % (command,outfile)
592
- if input:
593
- infile=tempfile.mktemp()
594
- open(infile," w" ).write(input)
595
- command=command+" < " +infile
596
- if capturestderr:
597
- errfile=tempfile.mktemp()
598
- command=command+" 2> " +errfile
599
- self.errorlevel=os.system(command) >> 8
600
- self.out=open(outfile," r" ).read()
601
- os.remove(outfile)
602
- if input:
603
- os.remove(infile)
604
- if capturestderr:
605
- self.err=open(errfile," r" ).read()
606
- os.remove(errfile)
607
-
608
- Note that many interactive programs (e.g. vi) don't work well with pipes
609
- substituted for standard input and output. You will have to use pseudo ttys
610
- (" ptys" ) instead of pipes. Or you can use a Python interface to Don Libes'
611
- " expect" library. A Python extension that interfaces to expect is called
612
- " expy" and available from https://expectpy.sourceforge.net. A pure Python
613
- solution that works like expect is ` pexpect
614
- < https://pypi.org/project/pexpect/> ` _.
615
-
616
-
617
537
How do I access the serial (RS232) port?
618
538
----------------------------------------
619
539
0 commit comments