Skip to content

Commit 193ce43

Browse files
committed
Paths, OS Commands
1 parent 37286e6 commit 193ce43

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

README.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,14 +1625,14 @@ def write_to_file(filename, text):
16251625
Paths
16261626
-----
16271627
```python
1628-
from os import getcwd, path, listdir, scandir
1629-
from glob import glob
1628+
import os, os.path as path, glob
1629+
from pathlib import Path
16301630
```
16311631

16321632
```python
1633-
<str> = getcwd() # Returns the current working directory.
1633+
<str> = os.getcwd() # Returns the current working directory.
16341634
<str> = path.join(<path>, ...) # Joins two or more pathname components.
1635-
<str> = path.abspath(<path>) # Returns absolute path.
1635+
<str> = path.realpath(<path>) # Resolves symlinks and calls path.abspath().
16361636
```
16371637

16381638
```python
@@ -1642,8 +1642,8 @@ from glob import glob
16421642
```
16431643

16441644
```python
1645-
<list> = listdir(path='.') # Returns filenames located at the path.
1646-
<list> = glob('<pattern>') # Returns paths matching the wildcard pattern.
1645+
<list> = os.listdir(path='.') # Returns filenames located at the path.
1646+
<list> = glob.glob('<pattern>') # Returns paths matching the wildcard pattern.
16471647
```
16481648

16491649
```python
@@ -1661,20 +1661,17 @@ from glob import glob
16611661
**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.**
16621662

16631663
```python
1664-
<iter> = scandir(path='.') # Returns DirEntry objects located at the path.
1664+
<iter> = os.scandir(path='.') # Returns DirEntry objects located at the path.
16651665
<str> = <DirEntry>.path # Returns the whole path as a string.
16661666
<str> = <DirEntry>.name # Returns final component as a string.
16671667
<file> = open(<DirEntry>) # Opens the file and returns a file object.
16681668
```
16691669

16701670
### Path Object
1671-
```python
1672-
from pathlib import Path
1673-
```
1674-
16751671
```python
16761672
<Path> = Path(<path> [, ...]) # Accepts strings, Paths and DirEntry objects.
16771673
<Path> = <path> / <path> [/ ...] # First or second path must be a Path object.
1674+
<Path> = <Path>.resolve() # Resolves symlinks and calls <Path>.absolute().
16781675
```
16791676

16801677
```python
@@ -1717,12 +1714,14 @@ os.makedirs(<path>, mode=0o777) # Creates all path's dirs. Also `exist_ok=Fa
17171714

17181715
```python
17191716
shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir.
1717+
shutil.copy2(from, to) # Also copies creation and modification time.
17201718
shutil.copytree(from, to) # Copies the directory. 'to' must not exist.
17211719
```
17221720

17231721
```python
17241722
os.rename(from, to) # Renames/moves the file or directory.
1725-
os.replace(from, to) # Same, but overwrites 'to' if it exists.
1723+
os.replace(from, to) # Same, but overwrites file 'to' even on Windows.
1724+
shutil.move(from, to) # Rename() that moves into 'to' if it's a dir.
17261725
```
17271726

17281727
```python

index.html

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<body>
5656
<header>
57-
<aside>April 11, 2023</aside>
57+
<aside>April 12, 2023</aside>
5858
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
5959
</header>
6060

@@ -1382,20 +1382,20 @@
13821382
file.write(text)
13831383
</code></pre></div>
13841384

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
13871387
</code></pre></div>
13881388

1389-
<pre><code class="python language-python hljs">&lt;str&gt; = getcwd() <span class="hljs-comment"># Returns the current working directory.</span>
1389+
<pre><code class="python language-python hljs">&lt;str&gt; = os.getcwd() <span class="hljs-comment"># Returns the current working directory.</span>
13901390
&lt;str&gt; = path.join(&lt;path&gt;, ...) <span class="hljs-comment"># Joins two or more pathname components.</span>
1391-
&lt;str&gt; = path.abspath(&lt;path&gt;) <span class="hljs-comment"># Returns absolute path.</span>
1391+
&lt;str&gt; = path.realpath(&lt;path&gt;) <span class="hljs-comment"># Resolves symlinks and calls path.abspath().</span>
13921392
</code></pre>
13931393
<pre><code class="python language-python hljs">&lt;str&gt; = path.basename(&lt;path&gt;) <span class="hljs-comment"># Returns final component of the path.</span>
13941394
&lt;str&gt; = path.dirname(&lt;path&gt;) <span class="hljs-comment"># Returns path without the final component.</span>
13951395
&lt;tup.&gt; = path.splitext(&lt;path&gt;) <span class="hljs-comment"># Splits on last period of the final component.</span>
13961396
</code></pre>
1397-
<pre><code class="python language-python hljs">&lt;list&gt; = listdir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns filenames located at the path.</span>
1398-
&lt;list&gt; = glob(<span class="hljs-string">'&lt;pattern&gt;'</span>) <span class="hljs-comment"># Returns paths matching the wildcard pattern.</span>
1397+
<pre><code class="python language-python hljs">&lt;list&gt; = os.listdir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns filenames located at the path.</span>
1398+
&lt;list&gt; = glob.glob(<span class="hljs-string">'&lt;pattern&gt;'</span>) <span class="hljs-comment"># Returns paths matching the wildcard pattern.</span>
13991399
</code></pre>
14001400
<pre><code class="python language-python hljs">&lt;bool&gt; = path.exists(&lt;path&gt;) <span class="hljs-comment"># Or: &lt;Path&gt;.exists()</span>
14011401
&lt;bool&gt; = path.isfile(&lt;path&gt;) <span class="hljs-comment"># Or: &lt;DirEntry/Path&gt;.is_file()</span>
@@ -1404,19 +1404,18 @@
14041404
<pre><code class="python language-python hljs">&lt;stat&gt; = os.stat(&lt;path&gt;) <span class="hljs-comment"># Or: &lt;DirEntry/Path&gt;.stat()</span>
14051405
&lt;real&gt; = &lt;stat&gt;.st_mtime/st_size/… <span class="hljs-comment"># Modification time, size in bytes, ...</span>
14061406
</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">&lt;iter&gt; = 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">&lt;iter&gt; = os.scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns DirEntry objects located at the path.</span>
14081408
&lt;str&gt; = &lt;DirEntry&gt;.path <span class="hljs-comment"># Returns the whole path as a string.</span>
14091409
&lt;str&gt; = &lt;DirEntry&gt;.name <span class="hljs-comment"># Returns final component as a string.</span>
14101410
&lt;file&gt; = open(&lt;DirEntry&gt;) <span class="hljs-comment"># Opens the file and returns a file object.</span>
14111411
</code></pre></div>
14121412

14131413

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">&lt;Path&gt; = Path(&lt;path&gt; [, ...]) <span class="hljs-comment"># Accepts strings, Paths and DirEntry objects.</span>
1415+
&lt;Path&gt; = &lt;path&gt; / &lt;path&gt; [/ ...] <span class="hljs-comment"># First or second path must be a Path object.</span>
1416+
&lt;Path&gt; = &lt;Path&gt;.resolve() <span class="hljs-comment"># Resolves symlinks and calls &lt;Path&gt;.absolute().</span>
14151417
</code></pre></div>
14161418

1417-
<pre><code class="python language-python hljs">&lt;Path&gt; = Path(&lt;path&gt; [, ...]) <span class="hljs-comment"># Accepts strings, Paths and DirEntry objects.</span>
1418-
&lt;Path&gt; = &lt;path&gt; / &lt;path&gt; [/ ...] <span class="hljs-comment"># First or second path must be a Path object.</span>
1419-
</code></pre>
14201419
<pre><code class="python language-python hljs">&lt;Path&gt; = Path() <span class="hljs-comment"># Returns relative cwd. Also Path('.').</span>
14211420
&lt;Path&gt; = Path.cwd() <span class="hljs-comment"># Returns absolute cwd. Also Path().resolve().</span>
14221421
&lt;Path&gt; = Path.home() <span class="hljs-comment"># Returns user's home directory (absolute).</span>
@@ -1442,10 +1441,12 @@
14421441
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>
14431442
</code></pre>
14441443
<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>
14451445
shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. 'to' must not exist.</span>
14461446
</code></pre>
14471447
<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>
14491450
</code></pre>
14501451
<pre><code class="python language-python hljs">os.remove(&lt;path&gt;) <span class="hljs-comment"># Deletes the file.</span>
14511452
os.rmdir(&lt;path&gt;) <span class="hljs-comment"># Deletes the empty directory.</span>
@@ -2934,7 +2935,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29342935

29352936

29362937
<footer>
2937-
<aside>April 11, 2023</aside>
2938+
<aside>April 12, 2023</aside>
29382939
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29392940
</footer>
29402941

parse.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@ const DATACLASS =
8888

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

9394
const OS_RENAME =
9495
'os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span>\n' +
95-
'os.replace(from, to) <span class="hljs-comment"># Same, but overwrites \'to\' if it exists.</span>\n';
96+
'os.replace(from, to) <span class="hljs-comment"># Same, but overwrites file \'to\' even on Windows.</span>\n' +
97+
'shutil.move(from, to) <span class="hljs-comment"># Rename() that moves into \'to\' if it\'s a dir.</span>\n';
9698

9799
const STRUCT_FORMAT =
98100
'<span class="hljs-section">\'&lt;n&gt;s\'</span><span class="hljs-attribute"></span>';

0 commit comments

Comments
 (0)