|
1443 | 1443 | file.write(text)
|
1444 | 1444 | </code></pre></div>
|
1445 | 1445 |
|
1446 |
| -<div><h2 id="path"><a href="#path" name="path">#</a>Path</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> os <span class="hljs-keyword">import</span> getcwd, path, listdir |
| 1446 | +<div><h2 id="path"><a href="#path" name="path">#</a>Path</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> os <span class="hljs-keyword">import</span> getcwd, path, listdir, scandir |
1447 | 1447 | <span class="hljs-keyword">from</span> glob <span class="hljs-keyword">import</span> glob
|
1448 | 1448 | </code></pre></div>
|
1449 | 1449 |
|
|
1455 | 1455 | <str> = path.dirname(<path>) <span class="hljs-comment"># Returns path without final component.</span>
|
1456 | 1456 | <tup.> = path.splitext(<path>) <span class="hljs-comment"># Splits on last period of final component.</span>
|
1457 | 1457 | </code></pre>
|
1458 |
| -<pre><code class="python language-python hljs"><list> = listdir(<path>) <span class="hljs-comment"># Returns filenames located at path.</span> |
| 1458 | +<pre><code class="python language-python hljs"><list> = listdir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns filenames located at path.</span> |
1459 | 1459 | <list> = glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns paths matching the wildcard pattern.</span>
|
1460 | 1460 | </code></pre>
|
| 1461 | +<pre><code class="python language-python hljs"><bool> = path.exists(<path>) <span class="hljs-comment"># Or: <Path>.exists()</span> |
| 1462 | +<bool> = path.isfile(<path>) <span class="hljs-comment"># Or: <DirEntry/Path>.is_file()</span> |
| 1463 | +<bool> = path.isdir(<path>) <span class="hljs-comment"># Or: <DirEntry/Path>.is_dir()</span> |
| 1464 | +</code></pre> |
| 1465 | +<div><h3 id="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><code class="python language-python hljs"><iter> = scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns DirEntry objects located at path.</span> |
| 1466 | +</code></pre></div> |
| 1467 | + |
| 1468 | + |
| 1469 | +<pre><code class="python language-python hljs"><str> = <DirEntry>.path <span class="hljs-comment"># Returns path as a string.</span> |
| 1470 | +<str> = <DirEntry>.name <span class="hljs-comment"># Returns final component as a string.</span> |
| 1471 | +<file> = open(<DirEntry>) <span class="hljs-comment"># Opens the file and returns a file object.</span> |
| 1472 | +</code></pre> |
1461 | 1473 | <div><h3 id="pathobject">Path Object</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
|
1462 | 1474 | </code></pre></div>
|
1463 | 1475 |
|
|
1469 | 1481 | <Path> = <Path>.resolve() <span class="hljs-comment"># Returns absolute Path without symlinks.</span>
|
1470 | 1482 | </code></pre>
|
1471 | 1483 | <pre><code class="python language-python hljs"><Path> = <Path>.parent <span class="hljs-comment"># Returns Path without final component.</span>
|
1472 |
| -<str> = <Path>.name <span class="hljs-comment"># Returns final component as string.</span> |
| 1484 | +<str> = <Path>.name <span class="hljs-comment"># Returns final component as a string.</span> |
1473 | 1485 | <str> = <Path>.stem <span class="hljs-comment"># Returns final component without extension.</span>
|
1474 | 1486 | <str> = <Path>.suffix <span class="hljs-comment"># Returns final component's extension.</span>
|
1475 | 1487 | <tup.> = <Path>.parts <span class="hljs-comment"># Returns all components as strings.</span>
|
1476 | 1488 | </code></pre>
|
1477 |
| -<pre><code class="python language-python hljs"><iter> = <Path>.iterdir() <span class="hljs-comment"># Returns dir contents as Path objects.</span> |
1478 |
| -<iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns Paths matching the wildcard pattern.</span> |
1479 |
| -</code></pre> |
1480 |
| -<pre><code class="python language-python hljs"><bool> = <Path>.exists() <span class="hljs-comment"># Or: path.exists(<path>)</span> |
1481 |
| -<bool> = <Path>.is_file() <span class="hljs-comment"># Or: path.isfile(<path>)</span> |
1482 |
| -<bool> = <Path>.is_dir() <span class="hljs-comment"># Or: path.isdir(<path>)</span> |
1483 |
| -</code></pre> |
1484 | 1489 | <pre><code class="python language-python hljs"><str> = str(<Path>) <span class="hljs-comment"># Returns Path as a string.</span>
|
1485 | 1490 | <file> = open(<Path>) <span class="hljs-comment"># Opens the file and returns a file object.</span>
|
1486 | 1491 | </code></pre>
|
1487 |
| -<div><h3 id="direntry">DirEntry</h3><p><strong>Using scandir() instead of listdir() or iterdir() can significantly increase the performance of code that also needs file type or file attribute information.</strong></p><pre><code class="python language-python hljs"><iter> = os.scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns DirEntry objects located at path.</span> |
1488 |
| -</code></pre></div> |
1489 |
| - |
1490 |
| - |
1491 |
| -<pre><code class="python language-python hljs"><bool> = <DirEntry>.is_file() |
1492 |
| -<bool> = <DirEntry>.is_dir() |
1493 |
| -</code></pre> |
1494 |
| -<pre><code class="python language-python hljs"><str> = <DirEntry>.path <span class="hljs-comment"># Returns relative path as a string.</span> |
1495 |
| -<str> = <DirEntry>.name <span class="hljs-comment"># Returns final component.</span> |
1496 |
| -</code></pre> |
1497 |
| -<pre><code class="python language-python hljs"><Path> = Path(<DirEntry>) <span class="hljs-comment"># Returns relative Path object.</span> |
1498 |
| -<file> = open(<DirEntry>) <span class="hljs-comment"># Opens the file and returns a file object.</span> |
| 1492 | +<pre><code class="python language-python hljs"><iter> = <Path>.iterdir() <span class="hljs-comment"># Returns dir contents as Path objects.</span> |
| 1493 | +<iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns Paths matching the wildcard pattern.</span> |
1499 | 1494 | </code></pre>
|
1500 | 1495 | <div><h2 id="oscommands"><a href="#oscommands" name="oscommands">#</a>OS Commands</h2><div><h3 id="filesanddirectories">Files and Directories</h3><ul>
|
1501 | 1496 | <li><strong>Paths can be either strings, Paths, or DirEntry objects.</strong></li>
|
|
0 commit comments