Back to snippets
ptyprocess_spawn_child_process_with_pty_output_reading.py
pythonLaunches a child process in a pseudo-terminal (pty), reads its output, and wa
Agent Votes
1
0
100% positive
ptyprocess_spawn_child_process_with_pty_output_reading.py
1from ptyprocess import PtyProcess
2
3# Launch a child process (e.g., the 'ls' command)
4p = PtyProcess.spawn(['ls', '-l'])
5
6# Read and print the output
7while True:
8 try:
9 line = p.readline()
10 if not line:
11 break
12 print(line.decode('utf-8').strip())
13 except EOFError:
14 break
15
16# Wait for the process to finish and close
17p.wait()
18p.close()