Skip to content

Commit 00189e4

Browse files
committed
Paths, OS Commands
1 parent ce09368 commit 00189e4

File tree

4 files changed

+42
-42
lines changed

4 files changed

+42
-42
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,13 +1631,13 @@ from pathlib import Path
16311631
```
16321632

16331633
```python
1634-
<str> = os.path.basename(<path>) # Returns path's final component, i.e. file/dir.
1635-
<str> = os.path.dirname(<path>) # Returns path with its final component removed.
1634+
<str> = os.path.basename(<path>) # Returns final component (filename or dirname).
1635+
<str> = os.path.dirname(<path>) # Returns the path without its final component.
16361636
<tup.> = os.path.splitext(<path>) # Splits on last period of the final component.
16371637
```
16381638

16391639
```python
1640-
<list> = os.listdir(path='.') # Returns all filenames located at the path.
1640+
<list> = os.listdir(path='.') # Returns all file/dirnames located at the path.
16411641
<list> = glob.glob('<pattern>') # Returns paths matching the wildcard pattern.
16421642
```
16431643

@@ -1648,8 +1648,8 @@ from pathlib import Path
16481648
```
16491649

16501650
```python
1651-
<stat> = os.stat(<path>) # File's status. Also <Path/DirEntry>.stat().
1652-
<num> = <stat>.st_mtime/st_size/# Returns modification time, size in bytes, etc.
1651+
<stat> = os.stat(<path>) # A status object. Also <Path/DirEntry>.stat().
1652+
<num> = <stat>.st_size/st_mtime/# Returns size in bytes, modification time, etc.
16531653
```
16541654

16551655
### DirEntry
@@ -1672,16 +1672,16 @@ from pathlib import Path
16721672
```python
16731673
<Path> = Path() # Returns current working dir. Also Path('.').
16741674
<Path> = Path.cwd() # Returns absolute CWD. Also Path().resolve().
1675-
<Path> = Path.home() # Returns user's home directory (absolute).
1675+
<Path> = Path.home() # Returns the user's absolute home directory.
16761676
<Path> = Path(__file__).resolve() # Returns module's path if CWD wasn't changed.
16771677
```
16781678

16791679
```python
1680-
<Path> = <Path>.parent # Returns Path without the final component.
1681-
<str> = <Path>.name # Returns path's final component as a string.
1682-
<str> = <Path>.suffix # Returns name's last extension, e.g. '.gz'.
1683-
<str> = <Path>.stem # Returns name without the last extension.
1684-
<tup.> = <Path>.parts # Returns all path's components as strings.
1680+
<Path> = <Path>.parent # Returns the path without its final component.
1681+
<str> = <Path>.name # Returns final component (filename or dirname).
1682+
<str> = <Path>.suffix # Returns the name's last extension, e.g. '.gz'.
1683+
<str> = <Path>.stem # Returns the name without its last extension.
1684+
<tup.> = <Path>.parts # Returns a tuple of all components as strings.
16851685
```
16861686

16871687
```python
@@ -1708,9 +1708,9 @@ os.makedirs(<path>, mode=0o777) # Creates all path's dirs. Also `exist_ok=False
17081708
```
17091709

17101710
```python
1711-
shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir.
1711+
shutil.copy(from, to) # Copies the file ('to' can exist or be a dir).
17121712
shutil.copy2(from, to) # Also copies creation and modification time.
1713-
shutil.copytree(from, to) # Copies the directory. 'to' must not exist.
1713+
shutil.copytree(from, to) # Copies the directory ('to' must not exist).
17141714
```
17151715

17161716
```python
@@ -1720,7 +1720,7 @@ shutil.move(from, to) # Rename() that moves into 'to' if it's a dir.
17201720
```
17211721

17221722
```python
1723-
os.remove(<path>) # Deletes the file. Or `pip3 install send2trash`.
1723+
os.remove(<path>) # Deletes file. Also `$ pip3 install send2trash`.
17241724
os.rmdir(<path>) # Deletes the empty directory or raises OSError.
17251725
shutil.rmtree(<path>) # Deletes the directory and all of its contents.
17261726
```
@@ -1729,9 +1729,9 @@ shutil.rmtree(<path>) # Deletes the directory and all of its contents
17291729

17301730
### Shell Commands
17311731
```python
1732-
<pipe> = os.popen('<commands>') # Executes commands in sh/cmd. Returns combined stdout.
1733-
<str> = <pipe>.read(size=-1) # Reads 'size' chars or until EOF. Also readline/s().
1734-
<int> = <pipe>.close() # Returns None if last command exited with returncode 0.
1732+
<pipe> = os.popen('<commands>') # Executes commands in sh/cmd. Also os.system().
1733+
<str> = <pipe>.read(size=-1) # Returns a combined stdout. Also readline/s().
1734+
<int> = <pipe>.close() # Returns None if last command exited with 0.
17351735
```
17361736

17371737
#### Sends "1 + 1" to the basic calculator and captures its output:
@@ -1958,7 +1958,7 @@ Bytes
19581958
```python
19591959
<bytes> = bytes(<coll_of_ints>) # Integers must be in range from 0 to 255.
19601960
<bytes> = bytes(<str>, 'utf-8') # Encodes the string. Also <str>.encode().
1961-
<bytes> = bytes.fromhex('<hex>') # Hex pairs can be separated by whitespaces.
1961+
<bytes> = bytes.fromhex('<hex>') # Hex pairs can be separated by whitespace.
19621962
<bytes> = <int>.to_bytes(n_bytes, …) # `byteorder='big/little', signed=False`.
19631963
```
19641964

index.html

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
<body>
5858
<header>
59-
<aside>November 5, 2025</aside>
59+
<aside>November 8, 2025</aside>
6060
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
6161
</header>
6262

@@ -1396,19 +1396,19 @@
13961396
&lt;str&gt; = os.path.join(&lt;path&gt;, ...) <span class="hljs-comment"># Uses os.sep to join strings or Path objects.</span>
13971397
&lt;str&gt; = os.path.realpath(&lt;path&gt;) <span class="hljs-comment"># Resolves symlinks and calls path.abspath().</span>
13981398
</code></pre>
1399-
<pre><code class="python language-python hljs">&lt;str&gt; = os.path.basename(&lt;path&gt;) <span class="hljs-comment"># Returns path's final component, i.e. file/dir.</span>
1400-
&lt;str&gt; = os.path.dirname(&lt;path&gt;) <span class="hljs-comment"># Returns path with its final component removed.</span>
1399+
<pre><code class="python language-python hljs">&lt;str&gt; = os.path.basename(&lt;path&gt;) <span class="hljs-comment"># Returns final component (filename or dirname).</span>
1400+
&lt;str&gt; = os.path.dirname(&lt;path&gt;) <span class="hljs-comment"># Returns the path without its final component.</span>
14011401
&lt;tup.&gt; = os.path.splitext(&lt;path&gt;) <span class="hljs-comment"># Splits on last period of the final component.</span>
14021402
</code></pre>
1403-
<pre><code class="python language-python hljs">&lt;list&gt; = 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">&lt;list&gt; = os.listdir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns all file/dirnames located at the path.</span>
14041404
&lt;list&gt; = glob.glob(<span class="hljs-string">'&lt;pattern&gt;'</span>) <span class="hljs-comment"># Returns paths matching the wildcard pattern.</span>
14051405
</code></pre>
14061406
<pre><code class="python language-python hljs">&lt;bool&gt; = os.path.exists(&lt;path&gt;) <span class="hljs-comment"># Checks if path exists. Also &lt;Path&gt;.exists().</span>
14071407
&lt;bool&gt; = os.path.isfile(&lt;path&gt;) <span class="hljs-comment"># Also &lt;Path&gt;.is_file() and &lt;DirEntry&gt;.is_file().</span>
14081408
&lt;bool&gt; = os.path.isdir(&lt;path&gt;) <span class="hljs-comment"># Also &lt;Path&gt;.is_dir() and &lt;DirEntry&gt;.is_dir().</span>
14091409
</code></pre>
1410-
<pre><code class="python language-python hljs">&lt;stat&gt; = os.stat(&lt;path&gt;) <span class="hljs-comment"># File's status. Also &lt;Path/DirEntry&gt;.stat().</span>
1411-
&lt;num&gt; = &lt;stat&gt;.st_mtime/st_size/… <span class="hljs-comment"># Returns modification time, size in bytes, etc.</span>
1410+
<pre><code class="python language-python hljs">&lt;stat&gt; = os.stat(&lt;path&gt;) <span class="hljs-comment"># A status object. Also &lt;Path/DirEntry&gt;.stat().</span>
1411+
&lt;num&gt; = &lt;stat&gt;.st_size/st_mtime/<span class="hljs-comment"># Returns size in bytes, modification time, etc.</span>
14121412
</code></pre>
14131413
<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">&lt;iter&gt; = os.scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns DirEntry objects located at the path.</span>
14141414
&lt;str&gt; = &lt;DirEntry&gt;.path <span class="hljs-comment"># Is absolute if 'path' argument was absolute.</span>
@@ -1424,14 +1424,14 @@
14241424

14251425
<pre><code class="python language-python hljs">&lt;Path&gt; = Path() <span class="hljs-comment"># Returns current working dir. Also Path('.').</span>
14261426
&lt;Path&gt; = Path.cwd() <span class="hljs-comment"># Returns absolute CWD. Also Path().resolve().</span>
1427-
&lt;Path&gt; = Path.home() <span class="hljs-comment"># Returns user's home directory (absolute).</span>
1427+
&lt;Path&gt; = Path.home() <span class="hljs-comment"># Returns the user's absolute home directory.</span>
14281428
&lt;Path&gt; = Path(__file__).resolve() <span class="hljs-comment"># Returns module's path if CWD wasn't changed.</span>
14291429
</code></pre>
1430-
<pre><code class="python language-python hljs">&lt;Path&gt; = &lt;Path&gt;.parent <span class="hljs-comment"># Returns Path without the final component.</span>
1431-
&lt;str&gt; = &lt;Path&gt;.name <span class="hljs-comment"># Returns path's final component as a string.</span>
1432-
&lt;str&gt; = &lt;Path&gt;.suffix <span class="hljs-comment"># Returns name's last extension, e.g. '.gz'.</span>
1433-
&lt;str&gt; = &lt;Path&gt;.stem <span class="hljs-comment"># Returns name without the last extension.</span>
1434-
&lt;tup.&gt; = &lt;Path&gt;.parts <span class="hljs-comment"># Returns all path's components as strings.</span>
1430+
<pre><code class="python language-python hljs">&lt;Path&gt; = &lt;Path&gt;.parent <span class="hljs-comment"># Returns the path without its final component.</span>
1431+
&lt;str&gt; = &lt;Path&gt;.name <span class="hljs-comment"># Returns final component (filename or dirname).</span>
1432+
&lt;str&gt; = &lt;Path&gt;.suffix <span class="hljs-comment"># Returns the name's last extension, e.g. '.gz'.</span>
1433+
&lt;str&gt; = &lt;Path&gt;.stem <span class="hljs-comment"># Returns the name without its last extension.</span>
1434+
&lt;tup.&gt; = &lt;Path&gt;.parts <span class="hljs-comment"># Returns a tuple of all components as strings.</span>
14351435
</code></pre>
14361436
<pre><code class="python language-python hljs">&lt;iter&gt; = &lt;Path&gt;.iterdir() <span class="hljs-comment"># Returns directory contents as Path objects.</span>
14371437
&lt;iter&gt; = &lt;Path&gt;.glob(<span class="hljs-string">'&lt;pattern&gt;'</span>) <span class="hljs-comment"># Returns Paths matching the wildcard pattern.</span>
@@ -1446,25 +1446,25 @@
14461446
os.mkdir(&lt;path&gt;, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates a directory. Permissions are in octal.</span>
14471447
os.makedirs(&lt;path&gt;, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates all path's dirs. Also `exist_ok=False`.</span>
14481448
</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>
14501450
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>
14521452
</code></pre>
14531453
<pre><code class="python language-python hljs">os.rename(from, to) <span class="hljs-comment"># Renames or moves the file or directory 'from'.</span>
14541454
os.replace(from, to) <span class="hljs-comment"># Same, but overwrites file 'to' even on Windows.</span>
14551455
shutil.move(from, to) <span class="hljs-comment"># Rename() that moves into 'to' if it's a dir.</span>
14561456
</code></pre>
1457-
<pre><code class="python language-python hljs">os.remove(&lt;path&gt;) <span class="hljs-comment"># Deletes the file. Or `pip3 install send2trash`.</span>
1457+
<pre><code class="python language-python hljs">os.remove(&lt;path&gt;) <span class="hljs-comment"># Deletes file. Also `$ pip3 install send2trash`.</span>
14581458
os.rmdir(&lt;path&gt;) <span class="hljs-comment"># Deletes the empty directory or raises OSError.</span>
14591459
shutil.rmtree(&lt;path&gt;) <span class="hljs-comment"># Deletes the directory and all of its contents.</span>
14601460
</code></pre>
14611461
<ul>
14621462
<li><strong>Paths can be either strings, Path objects, or DirEntry objects.</strong></li>
14631463
<li><strong>Functions report OS related errors by raising OSError or one of its <a href="#exceptions-1">subclasses</a>.</strong></li>
14641464
</ul>
1465-
<div><h3 id="shellcommands">Shell Commands</h3><pre><code class="python language-python hljs">&lt;pipe&gt; = os.popen(<span class="hljs-string">'&lt;commands&gt;'</span>) <span class="hljs-comment"># Executes commands in sh/cmd. Returns combined stdout.</span>
1466-
&lt;str&gt; = &lt;pipe&gt;.read(size=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Reads 'size' chars or until EOF. Also readline/s().</span>
1467-
&lt;int&gt; = &lt;pipe&gt;.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">&lt;pipe&gt; = os.popen(<span class="hljs-string">'&lt;commands&gt;'</span>) <span class="hljs-comment"># Executes commands in sh/cmd. Also os.system().</span>
1466+
&lt;str&gt; = &lt;pipe&gt;.read(size=<span class="hljs-number">-1</span>) <span class="hljs-comment"># Returns a combined stdout. Also readline/s().</span>
1467+
&lt;int&gt; = &lt;pipe&gt;.close() <span class="hljs-comment"># Returns None if last command exited with 0.</span>
14681468
</code></pre></div>
14691469

14701470
<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">&gt;&gt;&gt; </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,7 +1635,7 @@
16351635

16361636
<div><h3 id="encode-1">Encode</h3><pre><code class="python language-python hljs">&lt;bytes&gt; = bytes(&lt;coll_of_ints&gt;) <span class="hljs-comment"># Integers must be in range from 0 to 255.</span>
16371637
&lt;bytes&gt; = bytes(&lt;str&gt;, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Encodes the string. Also &lt;str&gt;.encode().</span>
1638-
&lt;bytes&gt; = bytes.fromhex(<span class="hljs-string">'&lt;hex&gt;'</span>) <span class="hljs-comment"># Hex pairs can be separated by whitespaces.</span>
1638+
&lt;bytes&gt; = bytes.fromhex(<span class="hljs-string">'&lt;hex&gt;'</span>) <span class="hljs-comment"># Hex pairs can be separated by whitespace.</span>
16391639
&lt;bytes&gt; = &lt;int&gt;.to_bytes(n_bytes, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span>
16401640
</code></pre></div>
16411641

@@ -2936,7 +2936,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29362936

29372937

29382938
<footer>
2939-
<aside>November 5, 2025</aside>
2939+
<aside>November 8, 2025</aside>
29402940
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29412941
</footer>
29422942

parse.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ const CONSTRUCTOR_OVERLOADING =
9090
' self.a = a\n';
9191

9292
const SHUTIL_COPY =
93-
'shutil.copy(from, to) <span class="hljs-comment"># Copies the file. \'to\' can exist or be a dir.</span>\n' +
93+
'shutil.copy(from, to) <span class="hljs-comment"># Copies the file (\'to\' can exist or be a dir).</span>\n' +
9494
'shutil.copy2(from, to) <span class="hljs-comment"># Also copies creation and modification time.</span>\n' +
95-
'shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. \'to\' must not exist.</span>\n';
95+
'shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory (\'to\' must not exist).</span>\n';
9696

9797
const OS_RENAME =
9898
'os.rename(from, to) <span class="hljs-comment"># Renames or moves the file or directory \'from\'.</span>\n' +

pdf/remove_links.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
'<strong>Use <code class="python hljs"><span class="hljs-string">\'logging.exception(&lt;str&gt;)\'</span></code> to log the passed message, followed by the full error message of the caught exception. For details about setting up the logger see <a href="#logging">Logging</a>.</strong>': '<strong>Use <code class="python hljs"><span class="hljs-string">\'logging.exception(&lt;str&gt;)\'</span></code> to log the passed message, followed by the full error message of the caught exception. For details about the logger setup see Logging (p. 31).</strong>',
2525
'<strong>Functions report OS related errors by raising OSError or one of its <a href="#exceptions-1">subclasses</a>.</strong>': '<strong>Functions report OS related errors by raising OSError or one of its subclasses (p. 23).</strong>',
2626
'<strong>Without the <code class="python hljs"><span class="hljs-string">\'newline=""\'</span></code> argument, every \'\\r\\n\' sequence that is embedded inside a quoted field will get converted to \'\\n\'! For details about newline argument see <a href="#open">Open</a>.</strong>': '<strong>Without the <code class="python hljs"><span class="hljs-string">\'newline=""\'</span></code> argument, every \'\\r\\n\' sequence that is embedded inside a quoted field will get converted to \'\\n\'! For details about newline arg. see Open (p. 22).</strong>',
27-
'<strong>To print the spreadsheet to the console use <a href="#table">Tabulate</a> or PrettyTable library.</strong>': '<strong>To print the spreadsheet to the console use Tabulate or PrettyTable library (p. 34).</strong>',
28-
'<strong>For XML and binary Excel files (xlsx, xlsm and xlsb) use <a href="#fileformats">Pandas</a> library.</strong>': '<strong>For XML and binary Excel files (xlsx, xlsm and xlsb) use Pandas library (p. 46).</strong>',
27+
'<strong>To print the spreadsheet to the console use either <a href="#table">Tabulate</a> or PrettyTable library.</strong>': '<strong>To print the spreadsheet to the console use either Tabulate or PrettyTable library (p. 34).</strong>',
28+
'<strong>For XML and binary Excel files (extensions xlsx, xlsm and xlsb) use <a href="#fileformats">Pandas</a> library.</strong>': '<strong>For XML and binary Excel files (extensions xlsx, xlsm and xlsb) use Pandas library (p. 46).</strong>',
2929
'<strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong>': '<strong>Bools will be stored and returned as ints and dates as ISO formatted strings (p. 9).</strong>',
3030
'<strong>ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be <a href="#pickle">pickable</a>, queues must be sent using executor\'s \'initargs\' and \'initializer\' parameters, and executor should only be reachable via <code class="python hljs"><span class="hljs-string">\'if __name__ == "__main__": ...\'</span></code>.</strong>': '<strong>ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be pickable, queues must be sent using executor\'s \'initargs\' and \'initializer\' parameters, and executor should only be reachable via <code class="python hljs"><span class="hljs-string">\'if __name__ == "__main__": ...\'</span></code>.</strong>',
3131
'<strong>Install a WSGI server like <a href="https://flask.palletsprojects.com/en/latest/deploying/waitress/">Waitress</a> and a HTTP server such as <a href="https://flask.palletsprojects.com/en/latest/deploying/nginx/">Nginx</a> for better security.</strong>': '<strong>Install a WSGI server like Waitress and a HTTP server such as Nginx for better security.</strong>',

0 commit comments

Comments
 (0)