Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98f1cf8

Browse files
committed
Shell commands
1 parent edf82f8 commit 98f1cf8

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,14 +1677,21 @@ import os
16771677
<str> = os.popen('<shell_command>').read()
16781678
```
16791679

1680-
#### Using subprocess:
1681-
```python
1682-
>>> import subprocess, shlex
1683-
>>> a = subprocess.run(shlex.split('ls -a'), stdout=subprocess.PIPE)
1684-
>>> a.stdout
1685-
b'.\n..\nfile1.txt\nfile2.txt\n'
1686-
>>> a.returncode
1687-
0
1680+
#### Sends '1 + 1' to calculator and captures its output:
1681+
```python
1682+
>>> from subprocess import run
1683+
>>> run('bc', input='1 + 1\n', capture_output=True, encoding='utf-8')
1684+
CompletedProcess(args='bc', returncode=0, stdout='2\n', stderr='')
1685+
```
1686+
1687+
#### Sends 'test.in' to calculator running in standard mode and saves its output to 'test.out':
1688+
```python
1689+
>>> from shlex import split
1690+
>>> os.popen('echo 1 + 1 > test.in')
1691+
>>> run(split('bc -s'), stdin=open('test.in'), stdout=open('test.out', 'w'))
1692+
CompletedProcess(args=['bc', '-s'], returncode=0)
1693+
>>> os.popen('cat test.out').read()
1694+
'2\n'
16881695
```
16891696

16901697

index.html

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,12 +1522,17 @@
15221522
&lt;str&gt; = os.popen(<span class="hljs-string">'&lt;shell_command&gt;'</span>).read()
15231523
</code></pre></div>
15241524

1525-
<div><h4 id="usingsubprocess">Using subprocess:</h4><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> subprocess, shlex
1526-
<span class="hljs-meta">&gt;&gt;&gt; </span>a = subprocess.run(shlex.split(<span class="hljs-string">'ls -a'</span>), stdout=subprocess.PIPE)
1527-
<span class="hljs-meta">&gt;&gt;&gt; </span>a.stdout
1528-
<span class="hljs-string">b'.\n..\nfile1.txt\nfile2.txt\n'</span>
1529-
<span class="hljs-meta">&gt;&gt;&gt; </span>a.returncode
1530-
<span class="hljs-number">0</span>
1525+
<div><h4 id="sends11tocalculatorandcapturesitsoutput">Sends '1 + 1' to calculator and captures its output:</h4><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> subprocess <span class="hljs-keyword">import</span> run
1526+
<span class="hljs-meta">&gt;&gt;&gt; </span>run(<span class="hljs-string">'bc'</span>, input=<span class="hljs-string">'1 + 1\n'</span>, capture_output=<span class="hljs-keyword">True</span>, encoding=<span class="hljs-string">'utf-8'</span>)
1527+
CompletedProcess(args=<span class="hljs-string">'bc'</span>, returncode=<span class="hljs-number">0</span>, stdout=<span class="hljs-string">'2\n'</span>, stderr=<span class="hljs-string">''</span>)
1528+
</code></pre></div>
1529+
1530+
<div><h4 id="sendstestintocalculatorrunninginstandardmodeandsavesitsoutputtotestout">Sends 'test.in' to calculator running in standard mode and saves its output to 'test.out':</h4><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> shlex <span class="hljs-keyword">import</span> split
1531+
<span class="hljs-meta">&gt;&gt;&gt; </span>os.popen(<span class="hljs-string">'echo 1 + 1 &gt; test.in'</span>)
1532+
<span class="hljs-meta">&gt;&gt;&gt; </span>run(split(<span class="hljs-string">'bc -s'</span>), stdin=open(<span class="hljs-string">'test.in'</span>), stdout=open(<span class="hljs-string">'test.out'</span>, <span class="hljs-string">'w'</span>))
1533+
CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-string">'-s'</span>], returncode=<span class="hljs-number">0</span>)
1534+
<span class="hljs-meta">&gt;&gt;&gt; </span>os.popen(<span class="hljs-string">'cat test.out'</span>).read()
1535+
<span class="hljs-string">'2\n'</span>
15311536
</code></pre></div>
15321537

15331538
<div><h2 id="json"><a href="#json" name="json">#</a>JSON</h2><p><strong>Text file format for storing collections of strings and numbers.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> json

0 commit comments

Comments
 (0)