Skip to content

Commit 7fa77b3

Browse files
committed
fixing custom exit and error handling
1 parent 5639a77 commit 7fa77b3

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

subprocess/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
Here are supporting materials for the Real Python tutorial, [The `subprocess` Module: Wrapping Programs With Python](https://realpython.com/python-subprocess/).
44

55
Be aware that some examples are designed for particular operating systems. The `basics_unix.py` file won't work on Windows, for instance.
6+
7+
[custom_exit.py](custom_exit.py)

subprocess/basics_unix.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
# LINUX examples
44

5-
subprocess.run("ls")
6-
subprocess.run("bash ls")
7-
subprocess.run(["bash", "ls"])
85
subprocess.run(["sh", "ls"])
9-
subprocess.run(["zsh", "ls"])
10-
subprocess.run(["ls"])
116

12-
subprocess.run("python helloworld.py")
7+
subprocess.run(["ls"], shell=True)

subprocess/custom_exit.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import sys
22

3+
print(sys.argv)
4+
35
try:
4-
raise SystemExit(sys.argv[1])
6+
raise SystemExit(int(sys.argv[1]))
57
except IndexError as e:
68
raise SystemExit(0) from e

subprocess/error_handling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
try:
5-
subprocess.run(["python", "timer.py", "5"], timeout=10)
5+
subprocess.run(["python", "timer.py", "10"], timeout=5, check=True)
66
except FileNotFoundError as exc:
77
print(f"Process failed because the executable could not be found.\n{exc}")
88
except subprocess.CalledProcessError as exc:

0 commit comments

Comments
 (0)