|
51 | 51 | "sh.ls('-l')"
|
52 | 52 | ]
|
53 | 53 | },
|
54 |
| - { |
55 |
| - "cell_type": "markdown", |
56 |
| - "metadata": {}, |
57 |
| - "source": [ |
58 |
| - "The output can be used by assigning the command to a variable, and using the result's `stdout` attribute. Note that the latter is a sequence of bytes, so it has to be decoded into a UTF-8 string for further processing." |
59 |
| - ] |
60 |
| - }, |
61 | 54 | {
|
62 | 55 | "cell_type": "code",
|
63 | 56 | "execution_count": null,
|
64 | 57 | "metadata": {},
|
65 | 58 | "outputs": [],
|
66 | 59 | "source": [
|
67 |
| - "cmd = sh.ls('-l', '-a', _encoding='UTF-8')" |
| 60 | + "result = sh.ls('-l', '-a')" |
68 | 61 | ]
|
69 | 62 | },
|
70 | 63 | {
|
|
73 | 66 | "metadata": {},
|
74 | 67 | "outputs": [],
|
75 | 68 | "source": [
|
76 |
| - "lines = cmd.stdout.decode(encoding='utf8').split('\\n')" |
| 69 | + "lines = result.split('\\n')" |
77 | 70 | ]
|
78 | 71 | },
|
79 | 72 | {
|
|
109 | 102 | "metadata": {},
|
110 | 103 | "outputs": [],
|
111 | 104 | "source": [
|
112 |
| - "sh.ls()" |
| 105 | + "for file in (line.strip() for line in sh.ls().split()):\n", |
| 106 | + " print(file)" |
113 | 107 | ]
|
114 | 108 | },
|
115 | 109 | {
|
|
179 | 173 | "metadata": {},
|
180 | 174 | "outputs": [],
|
181 | 175 | "source": [
|
182 |
| - "sh.cat('tmp/date_file.txt')" |
| 176 | + "print(sh.cat('tmp/date_file.txt'))" |
183 | 177 | ]
|
184 | 178 | },
|
185 | 179 | {
|
|
228 | 222 | },
|
229 | 223 | "outputs": [],
|
230 | 224 | "source": [
|
231 |
| - "sh.grep(sh.ls('-l'), r'\\.ipynb$')" |
| 225 | + "print(sh.grep('-e', r'\\.ipynb$', _in=sh.ls('-l')))" |
232 | 226 | ]
|
233 | 227 | },
|
234 | 228 | {
|
235 | 229 | "cell_type": "markdown",
|
236 | 230 | "metadata": {},
|
237 | 231 | "source": [
|
238 |
| - "Pipe the output of `cut` into `sort`." |
| 232 | + "Pipe the output of `cut` into `sort`. Also use the `_iter` argument to create a generator over standard output." |
239 | 233 | ]
|
240 | 234 | },
|
241 | 235 | {
|
|
246 | 240 | },
|
247 | 241 | "outputs": [],
|
248 | 242 | "source": [
|
249 |
| - "sh.sort(sh.cut('-d', ' ', '-f', '5', 'tmp/date_file.txt'), '-r')" |
| 243 | + "for line in sh.sort('-r', _in=sh.cut('-d', ' ', '-f', '5', 'tmp/date_file.txt'), _iter=True):\n", |
| 244 | + " print(line.strip())" |
250 | 245 | ]
|
251 | 246 | },
|
252 | 247 | {
|
|
315 | 310 | },
|
316 | 311 | "outputs": [],
|
317 | 312 | "source": [
|
| 313 | + "process = sh.sleep(10, _bg=True, _bg_exc=False, _timeout=3)\n", |
318 | 314 | "try:\n",
|
319 |
| - " process = sh.sleep(10, _bg=True, _timeout=3)\n", |
320 |
| - "except TimeoutError as error:\n", |
321 |
| - " print(error)" |
| 315 | + " process.wait()\n", |
| 316 | + "except sh.TimeoutException as error:\n", |
| 317 | + " print('process timed out')\n", |
| 318 | + " print(error.exit_code)" |
322 | 319 | ]
|
323 | 320 | },
|
324 | 321 | {
|
|
578 | 575 | "metadata": {},
|
579 | 576 | "outputs": [],
|
580 | 577 | "source": [
|
581 |
| - "process = subprocess.run('ls *.py', stdout=subprocess.PIPE, encoding='utf8', shell=True)" |
| 578 | + "process = subprocess.run('ls *.ipynb', stdout=subprocess.PIPE, encoding='utf8', shell=True)" |
582 | 579 | ]
|
583 | 580 | },
|
584 | 581 | {
|
|
653 | 650 | {
|
654 | 651 | "cell_type": "code",
|
655 | 652 | "execution_count": null,
|
656 |
| - "metadata": {}, |
| 653 | + "metadata": { |
| 654 | + "scrolled": true |
| 655 | + }, |
657 | 656 | "outputs": [],
|
658 | 657 | "source": [
|
659 | 658 | "process.returncode"
|
|
676 | 675 | "name": "python",
|
677 | 676 | "nbconvert_exporter": "python",
|
678 | 677 | "pygments_lexer": "ipython3",
|
679 |
| - "version": "3.11.3" |
| 678 | + "version": "3.11.6" |
680 | 679 | }
|
681 | 680 | },
|
682 | 681 | "nbformat": 4,
|
|
0 commit comments