-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopen.py
More file actions
30 lines (24 loc) · 698 Bytes
/
popen.py
File metadata and controls
30 lines (24 loc) · 698 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python3
# @file: popen.py
# @auth: sprax
# @date: 2020-04-01 13:35:50 Wed 01 Apr
import errno
from subprocess import PIPE, Popen
p = Popen('less', text=True,
stdin=PIPE)
for x in range(100):
line = 'Line number %d.\n' % x
try:
p.stdin.write(line)
except IOError as e:
if e.errno == errno.EPIPE or e.errno == errno.EINVAL:
# Stop loop on "Invalid pipe" or "Invalid argument".
# No sense in continuing with broken pipe.
break
else:
# Raise any other error.
raise
p.stdin.close()
p.wait()
# This should always be printed below any output written to less.
print('All done!')