Skip to content

Commit a12e7a6

Browse files
committed
populating readme
1 parent 5c2d2c4 commit a12e7a6

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

subprocess/README.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,80 @@ Here are supporting materials for the Real Python tutorial, [The `subprocess` Mo
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.
66

7-
[custom_exit.py](custom_exit.py)
7+
Below are quick descriptions of what the different script do.
8+
9+
## [basics_unix.py](basics_unix.py)
10+
11+
**Only works on Linux or macOS**
12+
13+
Demonstrates basic usage of `subprocess.run()`
14+
15+
## [basics_win.py](basics_win.py)
16+
17+
**Only works on Windows**
18+
19+
Demonstrates basic usage of `subprocess.run()`
20+
21+
## [custom_exit.py](custom_exit.py)
22+
23+
Raises custom exit codes:
24+
25+
```shell
26+
$ python custom_exit.py 3
27+
```
28+
29+
This will exit with error code `3`. By default, will exit with code `0`
30+
31+
## [exiter_run.py](exiter_run.py)
32+
33+
Calls [custom_exit.py](custom_exit.py) with `subprocess.run()` demonstrating the `check` argument to `.run()`.
34+
35+
## [error_handling.py](error_handling.py)
36+
37+
Demonstrates error handling with `subprocess.run()`, catching common errors that `subprocess.run()` can encounter. Try changing the commands in `subprocess.run()` to raise different errors.
38+
39+
## [timer.py](timer.py)
40+
41+
Accepts an integer argument and will sleep for the given time. For example:
42+
43+
```
44+
$ python timer.py 5
45+
Starting timer of 5 seconds
46+
.....Done!
47+
```
48+
49+
## [popen_pipe.py](popen_pipe.py)
50+
51+
**Only works on Linux or macOS**
52+
53+
Demonstrates using `subprocess.Popen()` to pipe one command into the other.
54+
55+
## [popen_timer.py](popen_timer.py)
56+
57+
Demonstrates `subprocess.Popen()` calling [timer.py](timer.py)
58+
59+
## [random_num_gen.py](random_num_gen.py)
60+
61+
Generates and prints a random number.
62+
63+
## [random_num_gen_check_output.py](random_num_gen_check_output.py)
64+
65+
Uses `subprocess.run()` to call [random_num_gen.py](random_num_gen.py) to generate a random number, read the output, and then print it.
66+
67+
## [reaction_game.py](reaction_game.py)
68+
69+
Tests your reaction times.
70+
71+
Note that this game can be cheated by pressing enter before the game prompts you to press enter.
72+
73+
## [reaction_game_v2.py](reaction_game_v2.py)
74+
75+
Tests your reaction times, but guards against cheating by pressing enter before the game prompts you to by making you input a random letter instead.
76+
77+
## [reaction_game_v2_hack.py](reaction_game_v2_hack.py)
78+
79+
Uses `subprocess.Popen()` to cheat the second version of the reaction game.
80+
81+
## [create_project.py](create_project.py)
82+
83+
Creates a Python project, complete with a virtual environment and initialized Git repository.

subprocess/basics_win.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import subprocess
22

33
# https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cmd
4-
subprocess.run("cmd /c dir")
54
subprocess.run("dir", shell=True) # COMSPEC env variable
6-
subprocess.run(["cmd.exe /c dir"])
5+
subprocess.run(["cmd.exe", "/c", "dir"])
76

87
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pwsh?view=powershell-7.2
98
subprocess.run(["powershell", "-Command", "ls"])
10-
subprocess.run(["pwsh", "-Command", "ls"])
119
subprocess.run(["python", "helloworld.py"])

subprocess/exiter_run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import subprocess
22

3-
subprocess.run(["python", "exiter.py"], check=False)
3+
subprocess.run(["python", "custom_exit.py", "5"], check=False)
44

55
try:
6-
subprocess.run(["python", "exiter.py"], check=True)
6+
subprocess.run(["python", "custom_exit.py", "5"], check=True)
77
except subprocess.CalledProcessError as exc:
88
print(exc)

0 commit comments

Comments
 (0)