|
54 | 54 |
|
55 | 55 | <body>
|
56 | 56 | <header>
|
57 |
| - <aside>April 11, 2023</aside> |
| 57 | + <aside>April 12, 2023</aside> |
58 | 58 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
59 | 59 | </header>
|
60 | 60 |
|
|
1382 | 1382 | file.write(text)
|
1383 | 1383 | </code></pre></div>
|
1384 | 1384 |
|
1385 |
| -<div><h2 id="paths"><a href="#paths" name="paths">#</a>Paths</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> os <span class="hljs-keyword">import</span> getcwd, path, listdir, scandir |
1386 |
| -<span class="hljs-keyword">from</span> glob <span class="hljs-keyword">import</span> glob |
| 1385 | +<div><h2 id="paths"><a href="#paths" name="paths">#</a>Paths</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> os, os.path <span class="hljs-keyword">as</span> path, glob |
| 1386 | +<span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path |
1387 | 1387 | </code></pre></div>
|
1388 | 1388 |
|
1389 |
| -<pre><code class="python language-python hljs"><str> = getcwd() <span class="hljs-comment"># Returns the current working directory.</span> |
| 1389 | +<pre><code class="python language-python hljs"><str> = os.getcwd() <span class="hljs-comment"># Returns the current working directory.</span> |
1390 | 1390 | <str> = path.join(<path>, ...) <span class="hljs-comment"># Joins two or more pathname components.</span>
|
1391 |
| -<str> = path.abspath(<path>) <span class="hljs-comment"># Returns absolute path.</span> |
| 1391 | +<str> = path.realpath(<path>) <span class="hljs-comment"># Resolves symlinks and calls path.abspath().</span> |
1392 | 1392 | </code></pre>
|
1393 | 1393 | <pre><code class="python language-python hljs"><str> = path.basename(<path>) <span class="hljs-comment"># Returns final component of the path.</span>
|
1394 | 1394 | <str> = path.dirname(<path>) <span class="hljs-comment"># Returns path without the final component.</span>
|
1395 | 1395 | <tup.> = path.splitext(<path>) <span class="hljs-comment"># Splits on last period of the final component.</span>
|
1396 | 1396 | </code></pre>
|
1397 |
| -<pre><code class="python language-python hljs"><list> = listdir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns filenames located at the path.</span> |
1398 |
| -<list> = glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns paths matching the wildcard pattern.</span> |
| 1397 | +<pre><code class="python language-python hljs"><list> = os.listdir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns filenames located at the path.</span> |
| 1398 | +<list> = glob.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns paths matching the wildcard pattern.</span> |
1399 | 1399 | </code></pre>
|
1400 | 1400 | <pre><code class="python language-python hljs"><bool> = path.exists(<path>) <span class="hljs-comment"># Or: <Path>.exists()</span>
|
1401 | 1401 | <bool> = path.isfile(<path>) <span class="hljs-comment"># Or: <DirEntry/Path>.is_file()</span>
|
|
1404 | 1404 | <pre><code class="python language-python hljs"><stat> = os.stat(<path>) <span class="hljs-comment"># Or: <DirEntry/Path>.stat()</span>
|
1405 | 1405 | <real> = <stat>.st_mtime/st_size/… <span class="hljs-comment"># Modification time, size in bytes, ...</span>
|
1406 | 1406 | </code></pre>
|
1407 |
| -<div><h3 id="direntry">DirEntry</h3><p><strong>Unlike listdir(), scandir() returns DirEntry objects that cache isfile, isdir and on Windows also stat information, thus significantly increasing the performance of code that requires it.</strong></p><pre><code class="python language-python hljs"><iter> = scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns DirEntry objects located at the path.</span> |
| 1407 | +<div><h3 id="direntry">DirEntry</h3><p><strong>Unlike listdir(), scandir() returns DirEntry objects that cache isfile, isdir and on Windows also stat information, thus significantly increasing the performance of code that requires it.</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 the path.</span> |
1408 | 1408 | <str> = <DirEntry>.path <span class="hljs-comment"># Returns the whole path as a string.</span>
|
1409 | 1409 | <str> = <DirEntry>.name <span class="hljs-comment"># Returns final component as a string.</span>
|
1410 | 1410 | <file> = open(<DirEntry>) <span class="hljs-comment"># Opens the file and returns a file object.</span>
|
1411 | 1411 | </code></pre></div>
|
1412 | 1412 |
|
1413 | 1413 |
|
1414 |
| -<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 |
| 1414 | +<div><h3 id="pathobject">Path Object</h3><pre><code class="python language-python hljs"><Path> = Path(<path> [, ...]) <span class="hljs-comment"># Accepts strings, Paths and DirEntry objects.</span> |
| 1415 | +<Path> = <path> / <path> [/ ...] <span class="hljs-comment"># First or second path must be a Path object.</span> |
| 1416 | +<Path> = <Path>.resolve() <span class="hljs-comment"># Resolves symlinks and calls <Path>.absolute().</span> |
1415 | 1417 | </code></pre></div>
|
1416 | 1418 |
|
1417 |
| -<pre><code class="python language-python hljs"><Path> = Path(<path> [, ...]) <span class="hljs-comment"># Accepts strings, Paths and DirEntry objects.</span> |
1418 |
| -<Path> = <path> / <path> [/ ...] <span class="hljs-comment"># First or second path must be a Path object.</span> |
1419 |
| -</code></pre> |
1420 | 1419 | <pre><code class="python language-python hljs"><Path> = Path() <span class="hljs-comment"># Returns relative cwd. Also Path('.').</span>
|
1421 | 1420 | <Path> = Path.cwd() <span class="hljs-comment"># Returns absolute cwd. Also Path().resolve().</span>
|
1422 | 1421 | <Path> = Path.home() <span class="hljs-comment"># Returns user's home directory (absolute).</span>
|
|
1442 | 1441 | os.makedirs(<path>, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates all path's dirs. Also `exist_ok=False`.</span>
|
1443 | 1442 | </code></pre>
|
1444 | 1443 | <pre><code class="python language-python hljs">shutil.copy(from, to) <span class="hljs-comment"># Copies the file. 'to' can exist or be a dir.</span>
|
| 1444 | +shutil.copy2(from, to) <span class="hljs-comment"># Also copies creation and modification time.</span> |
1445 | 1445 | shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. 'to' must not exist.</span>
|
1446 | 1446 | </code></pre>
|
1447 | 1447 | <pre><code class="python language-python hljs">os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span>
|
1448 |
| -os.replace(from, to) <span class="hljs-comment"># Same, but overwrites 'to' if it exists.</span> |
| 1448 | +os.replace(from, to) <span class="hljs-comment"># Same, but overwrites file 'to' even on Windows.</span> |
| 1449 | +shutil.move(from, to) <span class="hljs-comment"># Rename() that moves into 'to' if it's a dir.</span> |
1449 | 1450 | </code></pre>
|
1450 | 1451 | <pre><code class="python language-python hljs">os.remove(<path>) <span class="hljs-comment"># Deletes the file.</span>
|
1451 | 1452 | os.rmdir(<path>) <span class="hljs-comment"># Deletes the empty directory.</span>
|
@@ -2934,7 +2935,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2934 | 2935 |
|
2935 | 2936 |
|
2936 | 2937 | <footer>
|
2937 |
| - <aside>April 11, 2023</aside> |
| 2938 | + <aside>April 12, 2023</aside> |
2938 | 2939 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
2939 | 2940 | </footer>
|
2940 | 2941 |
|
|
0 commit comments