<div><h3id="direntry">DirEntry</h3><p><strong>Using scandir() instead of listdir() can significantly increase the performance of code that also needs file type information.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> os <spanclass="hljs-keyword">import</span> scandir
</code></pre></div>
<pre><codeclass="python language-python hljs"><iter> = scandir(path=<spanclass="hljs-string">'.'</span>) <spanclass="hljs-comment"># Returns DirEntry objects located at path.</span>
<div><h3id="direntry">DirEntry</h3><p><strong>Using scandir() instead of listdir() can significantly increase the performance of code that also needs file type information.</strong></p><pre><codeclass="python language-python hljs"><iter> = scandir(path=<spanclass="hljs-string">'.'</span>) <spanclass="hljs-comment"># Returns DirEntry objects located at path.</span>
<str> = <DirEntry>.path <spanclass="hljs-comment"># Returns whole path as a string.</span>
<str> = <DirEntry>.name <spanclass="hljs-comment"># Returns final component as a string.</span>
<file> = open(<DirEntry>) <spanclass="hljs-comment"># Opens the file and returns a file object.</span>
<div><h3id="shellcommands">Shell Commands</h3><pre><codeclass="python language-python hljs"><pipe> = os.popen(<spanclass="hljs-string">'<command>'</span>) <spanclass="hljs-comment"># Executes command in sh/cmd and returns its stdout pipe.</span>
<str> = <pipe>.read() <spanclass="hljs-comment"># Waits for EOF and returns result. Also readline/s().</span>
<int> = <pipe>.close() <spanclass="hljs-comment"># Closes the pipe. Returns None on success, int on error.</span>
</code></pre></div>
<div><h4id="sends11tothebasiccalculatorandcapturesitsoutput">Sends '1 + 1' to the basic calculator and captures its output:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span><spanclass="hljs-keyword">from</span> subprocess <spanclass="hljs-keyword">import</span> run
<div><h4id="sends11tothebasiccalculatorandcapturesitsoutput">Sends '1 + 1' to the basic calculator and captures its output:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>subprocess.run(<spanclass="hljs-string">'bc'</span>, input=<spanclass="hljs-string">'1 + 1\n'</span>, capture_output=<spanclass="hljs-keyword">True</span>, text=<spanclass="hljs-keyword">True</span>)
<div><h4id="sendstestintothebasiccalculatorrunninginstandardmodeandsavesitsoutputtotestout">Sends test.in to the basic calculator running in standard mode and saves its output to test.out:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span><spanclass="hljs-keyword">from</span> shlex <spanclass="hljs-keyword">import</span> split