**Using scandir() instead of listdir() can significantly increase the performance of code that also needs file type information.**
**Unlike listdir(), scandir() returns DirEntry objects that cache isfile, isdir and on Windows also stat information, thus significantly increasing the performance of code that requires it.**
```python
<iter> = scandir(path='.') # Returns DirEntry objects located at path.
<real> = <stat>.st_mtime/st_size/… <spanclass="hljs-comment"># Modification time, size in bytes, …</span>
</code></pre>
<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>
<div><h3id="direntry">DirEntry</h3><p><strong>Unlike listdir(), scandir() returns DirEntry objects that cache isfile, isdir and on Windows also stat information, thus significantly increasing the performance of code that requires it.</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>
@ -1436,25 +1436,25 @@ value = args.<name>
<div><h3id="filesanddirectories">Files and Directories</h3><ul>
<li><strong>Paths can be either strings, Paths or DirEntry objects.</strong></li>
<li><strong>Functions report OS related errors by raising either OSError or one of its <ahref="#exceptions-1">subclasses</a>.</strong></li>
</ul><pre><codeclass="python language-python hljs">os.chdir(<path>) <spanclass="hljs-comment"># Changes the current working directory.</span>
os.mkdir(<path>, mode=<spanclass="hljs-number">0o777</span>) <spanclass="hljs-comment"># Creates a directory. Mode is in octal.</span>
os.makedirs(<path>, mode=<spanclass="hljs-number">0o777</span>) <spanclass="hljs-comment"># Creates all directories in the path.</span>
</ul><pre><codeclass="python language-python hljs">os.chdir(<path>) <spanclass="hljs-comment"># Changes the current working directory.</span>
os.mkdir(<path>, mode=<spanclass="hljs-number">0o777</span>) <spanclass="hljs-comment"># Creates a directory. Mode is in octal.</span>
os.makedirs(<path>, mode=<spanclass="hljs-number">0o777</span>) <spanclass="hljs-comment"># Creates dirs in path. Also: `exist_ok=False`.</span>
</code></pre></div>
<pre><codeclass="python language-python hljs">shutil.copy(from, to) <spanclass="hljs-comment"># Copies the file. 'to' can exist or be a dir.</span>
shutil.copytree(from, to) <spanclass="hljs-comment"># Copies the directory. 'to' must not exist.</span>
<pre><codeclass="python language-python hljs">shutil.copy(from, to) <spanclass="hljs-comment"># Copies the file. 'to' can exist or be a dir.</span>
shutil.copytree(from, to) <spanclass="hljs-comment"># Copies the directory. 'to' must not exist.</span>
</code></pre>
<pre><codeclass="python language-python hljs">os.rename(from, to) <spanclass="hljs-comment"># Renames/moves the file or directory.</span>
os.replace(from, to) <spanclass="hljs-comment"># Same, but overwrites 'to' if it exists.</span>
<pre><codeclass="python language-python hljs">os.rename(from, to) <spanclass="hljs-comment"># Renames/moves the file or directory.</span>
os.replace(from, to) <spanclass="hljs-comment"># Same, but overwrites 'to' if it exists.</span>
</code></pre>
<pre><codeclass="python language-python hljs">os.remove(<path>) <spanclass="hljs-comment"># Deletes the file.</span>
os.rmdir(<path>) <spanclass="hljs-comment"># Deletes the empty directory.</span>
shutil.rmtree(<path>) <spanclass="hljs-comment"># Deletes the directory.</span>
<pre><codeclass="python language-python hljs">os.remove(<path>) <spanclass="hljs-comment"># Deletes the file.</span>
os.rmdir(<path>) <spanclass="hljs-comment"># Deletes the empty directory.</span>
shutil.rmtree(<path>) <spanclass="hljs-comment"># Deletes the directory.</span>
</code></pre>
<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(size=<spanclass="hljs-number">-1</span>) <spanclass="hljs-comment"># Reads 'size' chars or until EOF. Also readline/s().</span>
<int> = <pipe>.close() <spanclass="hljs-comment"># Closes the pipe. Returns None on success, int on error.</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(size=<spanclass="hljs-number">-1</span>) <spanclass="hljs-comment"># Reads 'size' chars or until EOF. 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>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>)