|
1451 | 1451 | <str> = path.join(<span class="hljs-string">'<path>'</span>, ...) <span class="hljs-comment"># Joins two or more pathname components.</span>
|
1452 | 1452 | <str> = path.abspath(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Return an absolute path.</span>
|
1453 | 1453 | </code></pre>
|
1454 |
| -<pre><code class="python language-python hljs"><bool> = path.exists(<span class="hljs-string">'<path>'</span>) |
1455 |
| -<bool> = path.isfile(<span class="hljs-string">'<path>'</span>) |
1456 |
| -<bool> = path.isdir(<span class="hljs-string">'<path>'</span>) |
| 1454 | +<pre><code class="python language-python hljs"><str> = path.basename(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Returns final component.</span> |
| 1455 | +<str> = path.dirname(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Returns path without final component.</span> |
| 1456 | +<tup.> = path.splitext(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Splits on last period of final component.</span> |
1457 | 1457 | </code></pre>
|
1458 | 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>
|
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"><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> |
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 |
| 1461 | +<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 |
1466 | 1462 | </code></pre></div>
|
1467 | 1463 |
|
1468 |
| -<pre><code class="python language-python hljs"><Path> = Path(<span class="hljs-string">'<path>'</span> [, <span class="hljs-string">'<path>'</span>, <Path>, ...]) |
1469 |
| -<Path> = <Path> / <span class="hljs-string">'<dir>'</span> / <span class="hljs-string">'<file>'</span> |
| 1464 | +<pre><code class="python language-python hljs"><Path> = Path(<path> [, ...]) <span class="hljs-comment"># Accepts strings, Paths and DirEntry objects.</span> |
| 1465 | +<Path> = <path> / <path> [/ ...] <span class="hljs-comment"># One of the paths must be a Path object.</span> |
1470 | 1466 | </code></pre>
|
1471 | 1467 | <pre><code class="python language-python hljs"><Path> = Path() <span class="hljs-comment"># Or: Path('.')</span>
|
1472 | 1468 | <Path> = Path.cwd() <span class="hljs-comment"># Returns absolute cwd. Or: Path().resolve()</span>
|
1473 | 1469 | <Path> = <Path>.resolve() <span class="hljs-comment"># Returns absolute Path without symlinks.</span>
|
1474 | 1470 | <Path> = <Path>.parent <span class="hljs-comment"># Returns Path without final component.</span>
|
1475 | 1471 | </code></pre>
|
1476 |
| -<pre><code class="python language-python hljs"><bool> = <Path>.exists() |
1477 |
| -<bool> = <Path>.is_file() |
1478 |
| -<bool> = <Path>.is_dir() |
| 1472 | +<pre><code class="python language-python hljs"><bool> = <Path>.exists() <span class="hljs-comment"># Or: path.exists('<path>')</span> |
| 1473 | +<bool> = <Path>.is_file() <span class="hljs-comment"># Or: path.isfile('<path>')</span> |
| 1474 | +<bool> = <Path>.is_dir() <span class="hljs-comment"># Or: path.isdir('<path>')</span> |
1479 | 1475 | </code></pre>
|
1480 | 1476 | <pre><code class="python language-python hljs"><iter> = <Path>.iterdir() <span class="hljs-comment"># Returns dir contents as relative (not true) Path objects.</span>
|
1481 | 1477 | <iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns relative Paths matching the wildcard pattern.</span>
|
|
1488 | 1484 | </code></pre>
|
1489 | 1485 | <pre><code class="python language-python hljs"><file> = open(<Path>) <span class="hljs-comment"># Opens the file and returns a file object.</span>
|
1490 | 1486 | </code></pre>
|
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> |
| 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> |
1492 | 1488 | </code></pre></div>
|
1493 | 1489 |
|
1494 | 1490 |
|
|
0 commit comments