|
1413 | 1413 | <bool> = path.isfile(<span class="hljs-string">'<path>'</span>)
|
1414 | 1414 | <bool> = path.isdir(<span class="hljs-string">'<path>'</span>)
|
1415 | 1415 | </code></pre>
|
1416 |
| -<pre><code class="python language-python hljs"><list> = listdir(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># List of filenames located at 'path'.</span> |
| 1416 | +<pre><code class="python language-python hljs"><list> = listdir(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># List of filenames located at path.</span> |
1417 | 1417 | <list> = glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Filenames matching the wildcard pattern.</span>
|
1418 | 1418 | </code></pre>
|
1419 | 1419 | <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
|
|
1428 | 1428 | <bool> = <Path>.is_dir()
|
1429 | 1429 | </code></pre>
|
1430 | 1430 | <pre><code class="python language-python hljs"><iter> = <Path>.iterdir() <span class="hljs-comment"># Returns dir contents as Path objects.</span>
|
1431 |
| -<iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Paths matching the wildcard pattern.</span> |
| 1431 | +<iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns Paths matching the wildcard pattern.</span> |
1432 | 1432 | </code></pre>
|
1433 |
| -<pre><code class="python language-python hljs"><str> = str(<Path>) <span class="hljs-comment"># Returns path as a string.</span> |
1434 |
| -<tup.> = <Path>.parts <span class="hljs-comment"># Returns all components as strings.</span> |
1435 |
| -<Path> = <Path>.resolve() <span class="hljs-comment"># Returns absolute Path without symlinks.</span> |
1436 |
| -</code></pre> |
1437 |
| -<pre><code class="python language-python hljs"><str> = <Path>.name <span class="hljs-comment"># Final component.</span> |
| 1433 | +<pre><code class="python language-python hljs"><str> = str(<Path>) <span class="hljs-comment"># Path as a string.</span> |
| 1434 | +<str> = <Path>.name <span class="hljs-comment"># Final component.</span> |
1438 | 1435 | <str> = <Path>.stem <span class="hljs-comment"># Final component without extension.</span>
|
1439 | 1436 | <str> = <Path>.suffix <span class="hljs-comment"># Final component's extension.</span>
|
1440 |
| -<Path> = <Path>.parent <span class="hljs-comment"># Path without final component.</span> |
| 1437 | +<tup.> = <Path>.parts <span class="hljs-comment"># All components as strings.</span> |
| 1438 | +</code></pre> |
| 1439 | +<pre><code class="python language-python hljs"><Path> = <Path>.resolve() <span class="hljs-comment"># Returns absolute path without symlinks.</span> |
| 1440 | +<Path> = <Path>.parent <span class="hljs-comment"># Returns path without final component.</span> |
1441 | 1441 | </code></pre>
|
1442 | 1442 | <div><h2 id="commandexecution"><a href="#commandexecution" name="commandexecution">#</a>Command Execution</h2><div><h3 id="filesanddirectories">Files and Directories</h3><ul>
|
1443 | 1443 | <li><strong>Paths can be either strings or Path objects.</strong></li>
|
1444 | 1444 | <li><strong>All exceptions are either 'OSError' or its subclasses.</strong></li>
|
1445 | 1445 | </ul><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> os
|
1446 | 1446 | <str> = os.getcwd() <span class="hljs-comment"># Returns the current working directory.</span>
|
1447 |
| -os.chdir(<path>) <span class="hljs-comment"># Changes the current working directory.</span> |
| 1447 | +os.chdir(<path>) <span class="hljs-comment"># Changes current working directory.</span> |
1448 | 1448 | </code></pre></div></div>
|
1449 | 1449 |
|
1450 | 1450 |
|
|
1463 | 1463 | <bool> = <DirEntry>.is_dir()
|
1464 | 1464 | </code></pre></div>
|
1465 | 1465 |
|
1466 |
| -<pre><code class="python language-python hljs"><str> = <DirEntry>.name <span class="hljs-comment"># Final component of the path.</span> |
1467 |
| -<str> = <DirEntry>.path <span class="hljs-comment"># Path with final component.</span> |
| 1466 | +<pre><code class="python language-python hljs"><str> = <DirEntry>.path <span class="hljs-comment"># Path as a string.</span> |
| 1467 | +<str> = <DirEntry>.name <span class="hljs-comment"># Final component.</span> |
1468 | 1468 | <Path> = Path(<DirEntry>) <span class="hljs-comment"># Path object.</span>
|
1469 | 1469 | </code></pre>
|
1470 | 1470 | <div><h3 id="shellcommands">Shell Commands</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> os
|
|
0 commit comments