|
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 | +<str> = path.join(<path>, ...) <span class="hljs-comment"># Joins two or more pathname components.</span> |
| 1452 | +<str> = path.abspath(<path>) <span class="hljs-comment"># Return an absolute path.</span> |
1453 | 1453 | </code></pre>
|
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> |
| 1454 | +<pre><code class="python language-python hljs"><str> = path.basename(<path>) <span class="hljs-comment"># Returns final component.</span> |
| 1455 | +<str> = path.dirname(<path>) <span class="hljs-comment"># Returns path without final component.</span> |
| 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(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Returns filenames located at path.</span> |
| 1458 | +<pre><code class="python language-python hljs"><list> = listdir(<path>) <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 | 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
|
|
1465 | 1465 | <Path> = <path> / <path> [/ ...] <span class="hljs-comment"># One of the paths must be a Path object.</span>
|
1466 | 1466 | </code></pre>
|
1467 | 1467 | <pre><code class="python language-python hljs"><Path> = Path() <span class="hljs-comment"># Or: Path('.')</span>
|
1468 |
| -<Path> = Path.cwd() <span class="hljs-comment"># Returns absolute cwd. Or: Path().resolve()</span> |
1469 |
| -<Path> = <Path>.resolve() <span class="hljs-comment"># Returns absolute Path without symlinks.</span> |
| 1468 | +<Path> = Path.cwd() <span class="hljs-comment"># Returns the absolute cwd.</span> |
| 1469 | +<Path> = <Path>.resolve() <span class="hljs-comment"># Returns the absolute Path without symlinks.</span> |
1470 | 1470 | <Path> = <Path>.parent <span class="hljs-comment"># Returns Path without final component.</span>
|
1471 | 1471 | </code></pre>
|
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> |
1475 |
| -</code></pre> |
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> |
1477 |
| -<iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns relative Paths matching the wildcard pattern.</span> |
1478 |
| -</code></pre> |
1479 |
| -<pre><code class="python language-python hljs"><str> = str(<Path>) <span class="hljs-comment"># Returns Path as a string.</span> |
1480 |
| -<str> = <Path>.name <span class="hljs-comment"># Returns final component.</span> |
| 1472 | +<pre><code class="python language-python hljs"><str> = <Path>.name <span class="hljs-comment"># Returns final component.</span> |
1481 | 1473 | <str> = <Path>.stem <span class="hljs-comment"># Returns final component without extension.</span>
|
1482 | 1474 | <str> = <Path>.suffix <span class="hljs-comment"># Returns final component's extension.</span>
|
1483 | 1475 | <tup.> = <Path>.parts <span class="hljs-comment"># Returns all components as strings.</span>
|
1484 | 1476 | </code></pre>
|
1485 |
| -<pre><code class="python language-python hljs"><file> = open(<Path>) <span class="hljs-comment"># Opens the file and returns a file object.</span> |
| 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 | +<pre><code class="python language-python hljs"><str> = str(<Path>) <span class="hljs-comment"># Returns Path as a string.</span> |
| 1485 | +<file> = open(<Path>) <span class="hljs-comment"># Opens the file and returns a file object.</span> |
1486 | 1486 | </code></pre>
|
1487 | 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 | 1488 | </code></pre></div>
|
|
0 commit comments