|
1453 | 1453 | <bool> = path.isfile(<span class="hljs-string">'<path>'</span>)
|
1454 | 1454 | <bool> = path.isdir(<span class="hljs-string">'<path>'</span>)
|
1455 | 1455 | </code></pre>
|
1456 |
| -<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> |
1457 |
| -<list> = glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Filenames matching the wildcard pattern.</span> |
| 1456 | +<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 | +<list> = glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns paths matching the wildcard pattern.</span> |
1458 | 1458 | </code></pre>
|
1459 | 1459 | <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 | 1460 | </code></pre></div>
|
1461 | 1461 |
|
1462 |
| -<pre><code class="python language-python hljs">cwd = Path() |
1463 |
| -<Path> = Path(<span class="hljs-string">'<path>'</span> [, <span class="hljs-string">'<path>'</span>, <Path>, ...]) |
| 1462 | +<pre><code class="python language-python hljs"><Path> = Path(<span class="hljs-string">'<path>'</span> [, <span class="hljs-string">'<path>'</span>, <Path>, ...]) |
1464 | 1463 | <Path> = <Path> / <span class="hljs-string">'<dir>'</span> / <span class="hljs-string">'<file>'</span>
|
1465 | 1464 | </code></pre>
|
| 1465 | +<pre><code class="python language-python hljs"><Path> = Path() <span class="hljs-comment"># Or: Path('.')</span> |
| 1466 | +<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> |
| 1468 | +</code></pre> |
1466 | 1469 | <pre><code class="python language-python hljs"><bool> = <Path>.exists()
|
1467 | 1470 | <bool> = <Path>.is_file()
|
1468 | 1471 | <bool> = <Path>.is_dir()
|
1469 | 1472 | </code></pre>
|
1470 |
| -<pre><code class="python language-python hljs"><iter> = <Path>.iterdir() <span class="hljs-comment"># Returns dir contents as Path objects.</span> |
1471 |
| -<iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns Paths matching the wildcard pattern.</span> |
| 1473 | +<pre><code class="python language-python hljs"><iter> = <Path>.iterdir() <span class="hljs-comment"># Returns dir contents as relative (not true) Path objects.</span> |
| 1474 | +<iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns relative Paths matching the wildcard pattern.</span> |
1472 | 1475 | </code></pre>
|
1473 |
| -<pre><code class="python language-python hljs"><str> = str(<Path>) <span class="hljs-comment"># Path as a string.</span> |
1474 |
| -<str> = <Path>.name <span class="hljs-comment"># Final component.</span> |
1475 |
| -<str> = <Path>.stem <span class="hljs-comment"># Final component without extension.</span> |
1476 |
| -<str> = <Path>.suffix <span class="hljs-comment"># Final component's extension.</span> |
1477 |
| -<tup.> = <Path>.parts <span class="hljs-comment"># All components as strings.</span> |
| 1476 | +<pre><code class="python language-python hljs"><str> = str(<Path>) <span class="hljs-comment"># Returns Path as a string.</span> |
| 1477 | +<str> = <Path>.name <span class="hljs-comment"># Returns final component.</span> |
| 1478 | +<str> = <Path>.stem <span class="hljs-comment"># Returns final component without extension.</span> |
| 1479 | +<str> = <Path>.suffix <span class="hljs-comment"># Returns final component's extension.</span> |
| 1480 | +<tup.> = <Path>.parts <span class="hljs-comment"># Returns all components as strings.</span> |
1478 | 1481 | </code></pre>
|
1479 |
| -<pre><code class="python language-python hljs"><Path> = <Path>.resolve() <span class="hljs-comment"># Returns absolute path without symlinks.</span> |
1480 |
| -<Path> = <Path>.parent <span class="hljs-comment"># Returns path without final component.</span> |
1481 |
| -<file> = open(<Path>) <span class="hljs-comment"># Opens the file and returns a file object.</span> |
| 1482 | +<pre><code class="python language-python hljs"><file> = open(<Path>) <span class="hljs-comment"># Opens the file and returns a file object.</span> |
| 1483 | +</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> |
| 1485 | +</code></pre></div> |
| 1486 | + |
| 1487 | +<pre><code class="python language-python hljs"><bool> = <DirEntry>.is_file() |
| 1488 | +<bool> = <DirEntry>.is_dir() |
| 1489 | +</code></pre> |
| 1490 | +<pre><code class="python language-python hljs"><str> = <DirEntry>.path <span class="hljs-comment"># Returns relative path as a string.</span> |
| 1491 | +<str> = <DirEntry>.name <span class="hljs-comment"># Returns final component.</span> |
| 1492 | +</code></pre> |
| 1493 | +<pre><code class="python language-python hljs"><Path> = Path(<DirEntry>) <span class="hljs-comment"># Returns relative Path object.</span> |
| 1494 | +<file> = open(<DirEntry>) <span class="hljs-comment"># Opens the file and returns a file object.</span> |
1482 | 1495 | </code></pre>
|
1483 | 1496 | <div><h2 id="oscommands"><a href="#oscommands" name="oscommands">#</a>OS Commands</h2><div><h3 id="filesanddirectories">Files and Directories</h3><ul>
|
1484 | 1497 | <li><strong>Paths can be either strings, Paths, or DirEntry objects.</strong></li>
|
|
1492 | 1505 | os.mkdir(<path>, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates a directory. Mode is in octal.</span>
|
1493 | 1506 | </code></pre>
|
1494 | 1507 | <pre><code class="python language-python hljs">shutil.copy(from, to) <span class="hljs-comment"># Copies the file.</span>
|
1495 |
| -shutil.copytree(from, to) <span class="hljs-comment"># Copies the entire directory tree.</span> |
| 1508 | +shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory.</span> |
1496 | 1509 | </code></pre>
|
1497 | 1510 | <pre><code class="python language-python hljs">os.rename(from, to) <span class="hljs-comment"># Renames the file or directory.</span>
|
1498 | 1511 | os.replace(from, to) <span class="hljs-comment"># Same, but overwrites 'to' if it exists.</span>
|
1499 | 1512 | </code></pre>
|
1500 | 1513 | <pre><code class="python language-python hljs">os.remove(<path>) <span class="hljs-comment"># Deletes the file.</span>
|
1501 | 1514 | os.rmdir(<path>) <span class="hljs-comment"># Deletes empty directory.</span>
|
1502 |
| -shutil.rmtree(<path>) <span class="hljs-comment"># Deletes the entire directory tree.</span> |
1503 |
| -</code></pre> |
1504 |
| -<pre><code class="python language-python hljs"><iter> = os.scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns os.DirEntry objects located at path.</span> |
1505 |
| -</code></pre> |
1506 |
| -<div><h4 id="direntry">DirEntry:</h4><pre><code class="python language-python hljs"><bool> = <DirEntry>.is_file() |
1507 |
| -<bool> = <DirEntry>.is_dir() |
1508 |
| -</code></pre></div> |
1509 |
| - |
1510 |
| -<pre><code class="python language-python hljs"><str> = <DirEntry>.path <span class="hljs-comment"># Path as a string.</span> |
1511 |
| -<str> = <DirEntry>.name <span class="hljs-comment"># Final component.</span> |
1512 |
| -</code></pre> |
1513 |
| -<pre><code class="python language-python hljs"><Path> = Path(<DirEntry>) <span class="hljs-comment"># Path object.</span> |
1514 |
| -<file> = open(<DirEntry>) <span class="hljs-comment"># File object.</span> |
| 1515 | +shutil.rmtree(<path>) <span class="hljs-comment"># Deletes non-empty directory.</span> |
1515 | 1516 | </code></pre>
|
1516 | 1517 | <div><h3 id="shellcommands">Shell Commands</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> os
|
1517 | 1518 | <str> = os.popen(<span class="hljs-string">'<shell_command>'</span>).read()
|
|
0 commit comments