|
56 | 56 |
|
57 | 57 | <body> |
58 | 58 | <header> |
59 | | - <aside>November 5, 2025</aside> |
| 59 | + <aside>November 8, 2025</aside> |
60 | 60 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a> |
61 | 61 | </header> |
62 | 62 |
|
|
1396 | 1396 | <str> = os.path.join(<path>, ...) <span class="hljs-comment"># Uses os.sep to join strings or Path objects.</span> |
1397 | 1397 | <str> = os.path.realpath(<path>) <span class="hljs-comment"># Resolves symlinks and calls path.abspath().</span> |
1398 | 1398 | </code></pre> |
1399 | | -<pre><code class="python language-python hljs"><str> = os.path.basename(<path>) <span class="hljs-comment"># Returns path's final component, i.e. file/dir.</span> |
1400 | | -<str> = os.path.dirname(<path>) <span class="hljs-comment"># Returns path with its final component removed.</span> |
| 1399 | +<pre><code class="python language-python hljs"><str> = os.path.basename(<path>) <span class="hljs-comment"># Returns final component (filename or dirname).</span> |
| 1400 | +<str> = os.path.dirname(<path>) <span class="hljs-comment"># Returns the path without its final component.</span> |
1401 | 1401 | <tup.> = os.path.splitext(<path>) <span class="hljs-comment"># Splits on last period of the final component.</span> |
1402 | 1402 | </code></pre> |
1403 | | -<pre><code class="python language-python hljs"><list> = os.listdir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns all filenames located at the path.</span> |
| 1403 | +<pre><code class="python language-python hljs"><list> = os.listdir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns all file/dirnames located at the path.</span> |
1404 | 1404 | <list> = glob.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns paths matching the wildcard pattern.</span> |
1405 | 1405 | </code></pre> |
1406 | 1406 | <pre><code class="python language-python hljs"><bool> = os.path.exists(<path>) <span class="hljs-comment"># Checks if path exists. Also <Path>.exists().</span> |
1407 | 1407 | <bool> = os.path.isfile(<path>) <span class="hljs-comment"># Also <Path>.is_file() and <DirEntry>.is_file().</span> |
1408 | 1408 | <bool> = os.path.isdir(<path>) <span class="hljs-comment"># Also <Path>.is_dir() and <DirEntry>.is_dir().</span> |
1409 | 1409 | </code></pre> |
1410 | | -<pre><code class="python language-python hljs"><stat> = os.stat(<path>) <span class="hljs-comment"># File's status. Also <Path/DirEntry>.stat().</span> |
1411 | | -<num> = <stat>.st_mtime/st_size/… <span class="hljs-comment"># Returns modification time, size in bytes, etc.</span> |
| 1410 | +<pre><code class="python language-python hljs"><stat> = os.stat(<path>) <span class="hljs-comment"># A status object. Also <Path/DirEntry>.stat().</span> |
| 1411 | +<num> = <stat>.st_size/st_mtime/… <span class="hljs-comment"># Returns size in bytes, modification time, etc.</span> |
1412 | 1412 | </code></pre> |
1413 | 1413 | <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> |
1414 | 1414 | <str> = <DirEntry>.path <span class="hljs-comment"># Is absolute if 'path' argument was absolute.</span> |
|
1424 | 1424 |
|
1425 | 1425 | <pre><code class="python language-python hljs"><Path> = Path() <span class="hljs-comment"># Returns current working dir. Also Path('.').</span> |
1426 | 1426 | <Path> = Path.cwd() <span class="hljs-comment"># Returns absolute CWD. Also Path().resolve().</span> |
1427 | | -<Path> = Path.home() <span class="hljs-comment"># Returns user's home directory (absolute).</span> |
| 1427 | +<Path> = Path.home() <span class="hljs-comment"># Returns the user's absolute home directory.</span> |
1428 | 1428 | <Path> = Path(__file__).resolve() <span class="hljs-comment"># Returns module's path if CWD wasn't changed.</span> |
1429 | 1429 | </code></pre> |
1430 | | -<pre><code class="python language-python hljs"><Path> = <Path>.parent <span class="hljs-comment"># Returns Path without the final component.</span> |
1431 | | -<str> = <Path>.name <span class="hljs-comment"># Returns path's final component as a string.</span> |
1432 | | -<str> = <Path>.suffix <span class="hljs-comment"># Returns name's last extension, e.g. '.gz'.</span> |
1433 | | -<str> = <Path>.stem <span class="hljs-comment"># Returns name without the last extension.</span> |
1434 | | -<tup.> = <Path>.parts <span class="hljs-comment"># Returns all path's components as strings.</span> |
| 1430 | +<pre><code class="python language-python hljs"><Path> = <Path>.parent <span class="hljs-comment"># Returns the path without its final component.</span> |
| 1431 | +<str> = <Path>.name <span class="hljs-comment"># Returns final component (filename or dirname).</span> |
| 1432 | +<str> = <Path>.suffix <span class="hljs-comment"># Returns the name's last extension, e.g. '.gz'.</span> |
| 1433 | +<str> = <Path>.stem <span class="hljs-comment"># Returns the name without its last extension.</span> |
| 1434 | +<tup.> = <Path>.parts <span class="hljs-comment"># Returns a tuple of all components as strings.</span> |
1435 | 1435 | </code></pre> |
1436 | 1436 | <pre><code class="python language-python hljs"><iter> = <Path>.iterdir() <span class="hljs-comment"># Returns directory contents as Path objects.</span> |
1437 | 1437 | <iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns Paths matching the wildcard pattern.</span> |
|
1446 | 1446 | os.mkdir(<path>, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates a directory. Permissions are in octal.</span> |
1447 | 1447 | os.makedirs(<path>, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates all path's dirs. Also `exist_ok=False`.</span> |
1448 | 1448 | </code></pre> |
1449 | | -<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> |
| 1449 | +<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> |
1450 | 1450 | shutil.copy2(from, to) <span class="hljs-comment"># Also copies creation and modification time.</span> |
1451 | | -shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. 'to' must not exist.</span> |
| 1451 | +shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory ('to' must not exist).</span> |
1452 | 1452 | </code></pre> |
1453 | 1453 | <pre><code class="python language-python hljs">os.rename(from, to) <span class="hljs-comment"># Renames or moves the file or directory 'from'.</span> |
1454 | 1454 | os.replace(from, to) <span class="hljs-comment"># Same, but overwrites file 'to' even on Windows.</span> |
1455 | 1455 | shutil.move(from, to) <span class="hljs-comment"># Rename() that moves into 'to' if it's a dir.</span> |
1456 | 1456 | </code></pre> |
1457 | | -<pre><code class="python language-python hljs">os.remove(<path>) <span class="hljs-comment"># Deletes the file. Or `pip3 install send2trash`.</span> |
| 1457 | +<pre><code class="python language-python hljs">os.remove(<path>) <span class="hljs-comment"># Deletes file. Also `$ pip3 install send2trash`.</span> |
1458 | 1458 | os.rmdir(<path>) <span class="hljs-comment"># Deletes the empty directory or raises OSError.</span> |
1459 | 1459 | shutil.rmtree(<path>) <span class="hljs-comment"># Deletes the directory and all of its contents.</span> |
1460 | 1460 | </code></pre> |
1461 | 1461 | <ul> |
1462 | 1462 | <li><strong>Paths can be either strings, Path objects, or DirEntry objects.</strong></li> |
1463 | 1463 | <li><strong>Functions report OS related errors by raising OSError or one of its <a href="#exceptions-1">subclasses</a>.</strong></li> |
1464 | 1464 | </ul> |
1465 | | -<div><h3 id="shellcommands">Shell Commands</h3><pre><code class="python language-python hljs"><pipe> = os.popen(<span class="hljs-string">'<commands>'</span>) <span class="hljs-comment"># Executes commands in sh/cmd. Returns combined stdout.</span> |
1466 | | -<str> = <pipe>.read(size=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Reads 'size' chars or until EOF. Also readline/s().</span> |
1467 | | -<int> = <pipe>.close() <span class="hljs-comment"># Returns None if last command exited with returncode 0.</span> |
| 1465 | +<div><h3 id="shellcommands">Shell Commands</h3><pre><code class="python language-python hljs"><pipe> = os.popen(<span class="hljs-string">'<commands>'</span>) <span class="hljs-comment"># Executes commands in sh/cmd. Also os.system().</span> |
| 1466 | +<str> = <pipe>.read(size=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Returns a combined stdout. Also readline/s().</span> |
| 1467 | +<int> = <pipe>.close() <span class="hljs-comment"># Returns None if last command exited with 0.</span> |
1468 | 1468 | </code></pre></div> |
1469 | 1469 |
|
1470 | 1470 | <div><h4 id="sends11tothebasiccalculatorandcapturesitsoutput">Sends "1 + 1" to the basic calculator and captures its output:</h4><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>subprocess.run(<span class="hljs-string">'bc'</span>, input=<span class="hljs-string">'1 + 1\n'</span>, capture_output=<span class="hljs-keyword">True</span>, text=<span class="hljs-keyword">True</span>) |
|
1635 | 1635 |
|
1636 | 1636 | <div><h3 id="encode-1">Encode</h3><pre><code class="python language-python hljs"><bytes> = bytes(<coll_of_ints>) <span class="hljs-comment"># Integers must be in range from 0 to 255.</span> |
1637 | 1637 | <bytes> = bytes(<str>, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Encodes the string. Also <str>.encode().</span> |
1638 | | -<bytes> = bytes.fromhex(<span class="hljs-string">'<hex>'</span>) <span class="hljs-comment"># Hex pairs can be separated by whitespaces.</span> |
| 1638 | +<bytes> = bytes.fromhex(<span class="hljs-string">'<hex>'</span>) <span class="hljs-comment"># Hex pairs can be separated by whitespace.</span> |
1639 | 1639 | <bytes> = <int>.to_bytes(n_bytes, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span> |
1640 | 1640 | </code></pre></div> |
1641 | 1641 |
|
@@ -2936,7 +2936,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment |
2936 | 2936 |
|
2937 | 2937 |
|
2938 | 2938 | <footer> |
2939 | | - <aside>November 5, 2025</aside> |
| 2939 | + <aside>November 8, 2025</aside> |
2940 | 2940 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a> |
2941 | 2941 | </footer> |
2942 | 2942 |
|
|
0 commit comments