|
1448 | 1448 | </code></pre></div>
|
1449 | 1449 |
|
1450 | 1450 | <pre><code class="python language-python hljs"><str> = getcwd() <span class="hljs-comment"># Returns the current working directory.</span>
|
| 1451 | +<str> = path.join(<span class="hljs-string">'<path>'</span>, ...) <span class="hljs-comment"># Joins two or more pathname components.</span> |
| 1452 | +<str> = path.abspath(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Return an absolute path.</span> |
1451 | 1453 | </code></pre>
|
1452 | 1454 | <pre><code class="python language-python hljs"><bool> = path.exists(<span class="hljs-string">'<path>'</span>)
|
1453 | 1455 | <bool> = path.isfile(<span class="hljs-string">'<path>'</span>)
|
|
1456 | 1458 | <pre><code class="python language-python hljs"><list> = listdir(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Returns filenames located at path.</span>
|
1457 | 1459 | <list> = glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns paths matching the wildcard pattern.</span>
|
1458 | 1460 | </code></pre>
|
| 1461 | +<pre><code class="python language-python hljs"><str> = path.basename(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Returns final component.</span> |
| 1462 | +<str> = path.dirname(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Returns path without final component.</span> |
| 1463 | +<str> = path.splitext(<span class="hljs-string">'<path>'</span>)[<span class="hljs-number">1</span>] <span class="hljs-comment"># Returns final component's extension.</span> |
| 1464 | +</code></pre> |
1459 | 1465 | <div><h3 id="pathlib">Pathlib</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
|
1460 | 1466 | </code></pre></div>
|
1461 | 1467 |
|
1462 | 1468 | <pre><code class="python language-python hljs"><Path> = Path(<span class="hljs-string">'<path>'</span> [, <span class="hljs-string">'<path>'</span>, <Path>, ...])
|
1463 | 1469 | <Path> = <Path> / <span class="hljs-string">'<dir>'</span> / <span class="hljs-string">'<file>'</span>
|
1464 | 1470 | </code></pre>
|
1465 | 1471 | <pre><code class="python language-python hljs"><Path> = Path() <span class="hljs-comment"># Or: Path('.')</span>
|
| 1472 | +<Path> = Path.cwd() <span class="hljs-comment"># Returns absolute cwd. Or: Path().resolve()</span> |
1466 | 1473 | <Path> = <Path>.resolve() <span class="hljs-comment"># Returns absolute Path without symlinks.</span>
|
1467 |
| -<Path> = <Path>.parent <span class="hljs-comment"># Returns path without final component.</span> |
| 1474 | +<Path> = <Path>.parent <span class="hljs-comment"># Returns Path without final component.</span> |
1468 | 1475 | </code></pre>
|
1469 | 1476 | <pre><code class="python language-python hljs"><bool> = <Path>.exists()
|
1470 | 1477 | <bool> = <Path>.is_file()
|
|
1481 | 1488 | </code></pre>
|
1482 | 1489 | <pre><code class="python language-python hljs"><file> = open(<Path>) <span class="hljs-comment"># Opens the file and returns a file object.</span>
|
1483 | 1490 | </code></pre>
|
1484 |
| -<div><h3 id="direntry">DirEntry</h3><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> |
| 1491 | +<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 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> |
1485 | 1492 | </code></pre></div>
|
1486 | 1493 |
|
| 1494 | + |
1487 | 1495 | <pre><code class="python language-python hljs"><bool> = <DirEntry>.is_file()
|
1488 | 1496 | <bool> = <DirEntry>.is_dir()
|
1489 | 1497 | </code></pre>
|
|
0 commit comments