|
1381 | 1381 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'default=<el>'</span></code> to set the default value.</strong></li> |
1382 | 1382 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'type=FileType(<mode>)'</span></code> for files.</strong></li> |
1383 | 1383 | </ul> |
1384 | | -<div><h2 id="open"><a href="#open" name="open">#</a>Open</h2><p><strong>Opens a file and returns a corresponding file object.</strong></p><pre><code class="python language-python hljs"><file> = open(<span class="hljs-string">'<path>'</span>, mode=<span class="hljs-string">'r'</span>, encoding=<span class="hljs-keyword">None</span>, newline=<span class="hljs-keyword">None</span>) |
| 1384 | +<div><h2 id="open"><a href="#open" name="open">#</a>Open</h2><p><strong>Opens the file and returns a corresponding file object.</strong></p><pre><code class="python language-python hljs"><file> = open(<span class="hljs-string">'<path>'</span>, mode=<span class="hljs-string">'r'</span>, encoding=<span class="hljs-keyword">None</span>, newline=<span class="hljs-keyword">None</span>) |
1385 | 1385 | </code></pre></div> |
1386 | 1386 |
|
1387 | 1387 |
|
1388 | 1388 | <ul> |
1389 | 1389 | <li><strong><code class="python hljs"><span class="hljs-string">'encoding=None'</span></code> means default encoding is used, which is platform dependent. Best practice is to use <code class="python hljs"><span class="hljs-string">'encoding="utf-8"'</span></code> whenever possible.</strong></li> |
1390 | | -<li><strong><code class="python hljs"><span class="hljs-string">'newline=None'</span></code> means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.</strong></li> |
| 1390 | +<li><strong><code class="python hljs"><span class="hljs-string">'newline=None'</span></code> means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to the system's default line separator.</strong></li> |
1391 | 1391 | <li><strong><code class="python hljs"><span class="hljs-string">'newline=""'</span></code> means no conversions take place, but input is still broken into chunks by readline() and readlines() on either '\n', '\r' or '\r\n'.</strong></li> |
1392 | 1392 | </ul> |
1393 | 1393 | <div><h3 id="modes">Modes</h3><ul> |
|
1445 | 1445 | <bool> = path.isfile(<span class="hljs-string">'<path>'</span>) |
1446 | 1446 | <bool> = path.isdir(<span class="hljs-string">'<path>'</span>) |
1447 | 1447 | </code></pre> |
1448 | | -<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> |
1449 | | -<list> = glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Filenames matching the wildcard pattern.</span> |
| 1448 | +<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> |
| 1449 | +<list> = glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Filenames matching the wildcard pattern.</span> |
1450 | 1450 | </code></pre> |
1451 | 1451 | <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 |
1452 | 1452 | </code></pre></div> |
|
1459 | 1459 | <bool> = <Path>.is_file() |
1460 | 1460 | <bool> = <Path>.is_dir() |
1461 | 1461 | </code></pre> |
1462 | | -<pre><code class="python language-python hljs"><iter> = <Path>.iterdir() <span class="hljs-comment"># Returns dir contents as Path objects.</span> |
1463 | | -<iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns Paths matching the wildcard pattern.</span> |
| 1462 | +<pre><code class="python language-python hljs"><iter> = <Path>.iterdir() <span class="hljs-comment"># Returns dir contents as Path objects.</span> |
| 1463 | +<iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns Paths matching the wildcard pattern.</span> |
1464 | 1464 | </code></pre> |
1465 | | -<pre><code class="python language-python hljs"><str> = str(<Path>) <span class="hljs-comment"># Path as a string.</span> |
1466 | | -<str> = <Path>.name <span class="hljs-comment"># Final component.</span> |
1467 | | -<str> = <Path>.stem <span class="hljs-comment"># Final component without extension.</span> |
1468 | | -<str> = <Path>.suffix <span class="hljs-comment"># Final component's extension.</span> |
1469 | | -<tup.> = <Path>.parts <span class="hljs-comment"># All components as strings.</span> |
| 1465 | +<pre><code class="python language-python hljs"><str> = str(<Path>) <span class="hljs-comment"># Path as a string.</span> |
| 1466 | +<str> = <Path>.name <span class="hljs-comment"># Final component.</span> |
| 1467 | +<str> = <Path>.stem <span class="hljs-comment"># Final component without extension.</span> |
| 1468 | +<str> = <Path>.suffix <span class="hljs-comment"># Final component's extension.</span> |
| 1469 | +<tup.> = <Path>.parts <span class="hljs-comment"># All components as strings.</span> |
1470 | 1470 | </code></pre> |
1471 | | -<pre><code class="python language-python hljs"><Path> = <Path>.resolve() <span class="hljs-comment"># Returns absolute path without symlinks.</span> |
1472 | | -<Path> = <Path>.parent <span class="hljs-comment"># Returns path without final component.</span> |
1473 | | -<file> = open(<Path>) <span class="hljs-comment"># Opens the file and returns a file object.</span> |
| 1471 | +<pre><code class="python language-python hljs"><Path> = <Path>.resolve() <span class="hljs-comment"># Returns absolute path without symlinks.</span> |
| 1472 | +<Path> = <Path>.parent <span class="hljs-comment"># Returns path without final component.</span> |
| 1473 | +<file> = open(<Path>) <span class="hljs-comment"># Opens the file and returns a file object.</span> |
1474 | 1474 | </code></pre> |
1475 | 1475 | <div><h2 id="oscommands"><a href="#oscommands" name="oscommands">#</a>OS Commands</h2><div><h3 id="filesanddirectories">Files and Directories</h3><ul> |
1476 | 1476 | <li><strong>Paths can be either strings, Paths, or DirEntry objects.</strong></li> |
|
1480 | 1480 |
|
1481 | 1481 |
|
1482 | 1482 |
|
1483 | | -<pre><code class="python language-python hljs">os.chdir(<path>) <span class="hljs-comment"># Changes current working directory.</span> |
1484 | | -os.mkdir(<path>, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates a directory.</span> |
| 1483 | +<pre><code class="python language-python hljs">os.chdir(<path>) <span class="hljs-comment"># Changes current working directory.</span> |
| 1484 | +os.mkdir(<path>, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates a directory.</span> |
1485 | 1485 | </code></pre> |
1486 | | -<pre><code class="python language-python hljs">os.rename(from, to) <span class="hljs-comment"># Renames the file or directory.</span> |
1487 | | -os.replace(from, to) <span class="hljs-comment"># Same, but overwrites 'to' if it exists.</span> |
| 1486 | +<pre><code class="python language-python hljs">os.rename(from, to) <span class="hljs-comment"># Renames the file or directory.</span> |
| 1487 | +os.replace(from, to) <span class="hljs-comment"># Same, but overwrites 'to' if it exists.</span> |
1488 | 1488 | </code></pre> |
1489 | | -<pre><code class="python language-python hljs">os.remove(<path>) <span class="hljs-comment"># Deletes the file.</span> |
1490 | | -os.rmdir(<path>) <span class="hljs-comment"># Deletes empty directory.</span> |
1491 | | -shutil.rmtree(<path>) <span class="hljs-comment"># Deletes the entire directory tree.</span> |
| 1489 | +<pre><code class="python language-python hljs">os.remove(<path>) <span class="hljs-comment"># Deletes the file.</span> |
| 1490 | +os.rmdir(<path>) <span class="hljs-comment"># Deletes empty directory.</span> |
| 1491 | +shutil.rmtree(<path>) <span class="hljs-comment"># Deletes the entire directory tree.</span> |
1492 | 1492 | </code></pre> |
1493 | | -<pre><code class="python language-python hljs">shutil.copy(from, to) <span class="hljs-comment"># Copies the file.</span> |
1494 | | -shutil.copytree(from, to) <span class="hljs-comment"># Copies the entire directory tree.</span> |
| 1493 | +<pre><code class="python language-python hljs">shutil.copy(from, to) <span class="hljs-comment"># Copies the file.</span> |
| 1494 | +shutil.copytree(from, to) <span class="hljs-comment"># Copies the entire directory tree.</span> |
1495 | 1495 | </code></pre> |
1496 | | -<pre><code class="python language-python hljs"><str> = os.getcwd() <span class="hljs-comment"># Returns the current working directory.</span> |
1497 | | -<iter> = os.scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns os.DirEntry objects located at path.</span> |
| 1496 | +<pre><code class="python language-python hljs"><str> = os.getcwd() <span class="hljs-comment"># Returns the current working directory.</span> |
| 1497 | +<iter> = os.scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns os.DirEntry objects located at path.</span> |
1498 | 1498 | </code></pre> |
1499 | 1499 | <div><h4 id="direntry">DirEntry:</h4><pre><code class="python language-python hljs"><bool> = <DirEntry>.is_file() |
1500 | 1500 | <bool> = <DirEntry>.is_dir() |
1501 | 1501 | </code></pre></div> |
1502 | 1502 |
|
1503 | | -<pre><code class="python language-python hljs"><str> = <DirEntry>.path <span class="hljs-comment"># Path as a string.</span> |
1504 | | -<str> = <DirEntry>.name <span class="hljs-comment"># Final component.</span> |
| 1503 | +<pre><code class="python language-python hljs"><str> = <DirEntry>.path <span class="hljs-comment"># Path as a string.</span> |
| 1504 | +<str> = <DirEntry>.name <span class="hljs-comment"># Final component.</span> |
1505 | 1505 | </code></pre> |
1506 | | -<pre><code class="python language-python hljs"><Path> = Path(<DirEntry>) <span class="hljs-comment"># Path object.</span> |
1507 | | -<file> = open(<DirEntry>) <span class="hljs-comment"># File object.</span> |
| 1506 | +<pre><code class="python language-python hljs"><Path> = Path(<DirEntry>) <span class="hljs-comment"># Path object.</span> |
| 1507 | +<file> = open(<DirEntry>) <span class="hljs-comment"># File object.</span> |
1508 | 1508 | </code></pre> |
1509 | 1509 | <div><h3 id="shellcommands">Shell Commands</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> os |
1510 | 1510 | <str> = os.popen(<span class="hljs-string">'<shell_command>'</span>).read() |
|
0 commit comments