Browse Source

Paths, OS Commands

pull/109/merge
Jure Šorn 1 year ago
parent
commit
193ce43dbd
3 changed files with 29 additions and 27 deletions
  1. 23
      README.md
  2. 29
      index.html
  3. 4
      parse.js

23
README.md

@ -1625,14 +1625,14 @@ def write_to_file(filename, text):
Paths Paths
----- -----
```python ```python
from os import getcwd, path, listdir, scandir
from glob import glob
import os, os.path as path, glob
from pathlib import Path
``` ```
```python ```python
<str> = getcwd() # Returns the current working directory.
<str> = os.getcwd() # Returns the current working directory.
<str> = path.join(<path>, ...) # Joins two or more pathname components. <str> = path.join(<path>, ...) # Joins two or more pathname components.
<str> = path.abspath(<path>) # Returns absolute path.
<str> = path.realpath(<path>) # Resolves symlinks and calls path.abspath().
``` ```
```python ```python
@ -1642,8 +1642,8 @@ from glob import glob
``` ```
```python ```python
<list> = listdir(path='.') # Returns filenames located at the path.
<list> = glob('<pattern>') # Returns paths matching the wildcard pattern.
<list> = os.listdir(path='.') # Returns filenames located at the path.
<list> = glob.glob('<pattern>') # Returns paths matching the wildcard pattern.
``` ```
```python ```python
@ -1661,20 +1661,17 @@ from glob import glob
**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.** **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.**
```python ```python
<iter> = scandir(path='.') # Returns DirEntry objects located at the path.
<iter> = os.scandir(path='.') # Returns DirEntry objects located at the path.
<str> = <DirEntry>.path # Returns the whole path as a string. <str> = <DirEntry>.path # Returns the whole path as a string.
<str> = <DirEntry>.name # Returns final component as a string. <str> = <DirEntry>.name # Returns final component as a string.
<file> = open(<DirEntry>) # Opens the file and returns a file object. <file> = open(<DirEntry>) # Opens the file and returns a file object.
``` ```
### Path Object ### Path Object
```python
from pathlib import Path
```
```python ```python
<Path> = Path(<path> [, ...]) # Accepts strings, Paths and DirEntry objects. <Path> = Path(<path> [, ...]) # Accepts strings, Paths and DirEntry objects.
<Path> = <path> / <path> [/ ...] # First or second path must be a Path object. <Path> = <path> / <path> [/ ...] # First or second path must be a Path object.
<Path> = <Path>.resolve() # Resolves symlinks and calls <Path>.absolute().
``` ```
```python ```python
@ -1717,12 +1714,14 @@ os.makedirs(<path>, mode=0o777) # Creates all path's dirs. Also `exist_ok=Fa
```python ```python
shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir. shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir.
shutil.copy2(from, to) # Also copies creation and modification time.
shutil.copytree(from, to) # Copies the directory. 'to' must not exist. shutil.copytree(from, to) # Copies the directory. 'to' must not exist.
``` ```
```python ```python
os.rename(from, to) # Renames/moves the file or directory. os.rename(from, to) # Renames/moves the file or directory.
os.replace(from, to) # Same, but overwrites 'to' if it exists.
os.replace(from, to) # Same, but overwrites file 'to' even on Windows.
shutil.move(from, to) # Rename() that moves into 'to' if it's a dir.
``` ```
```python ```python

29
index.html

@ -54,7 +54,7 @@
<body> <body>
<header> <header>
<aside>April 11, 2023</aside>
<aside>April 12, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header> </header>
@ -1382,20 +1382,20 @@ value = args.&lt;name&gt;
file.write(text) file.write(text)
</code></pre></div> </code></pre></div>
<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
<span class="hljs-keyword">from</span> glob <span class="hljs-keyword">import</span> glob
<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
<span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
</code></pre></div> </code></pre></div>
<pre><code class="python language-python hljs">&lt;str&gt; = getcwd() <span class="hljs-comment"># Returns the current working directory.</span>
<pre><code class="python language-python hljs">&lt;str&gt; = os.getcwd() <span class="hljs-comment"># Returns the current working directory.</span>
&lt;str&gt; = path.join(&lt;path&gt;, ...) <span class="hljs-comment"># Joins two or more pathname components.</span> &lt;str&gt; = path.join(&lt;path&gt;, ...) <span class="hljs-comment"># Joins two or more pathname components.</span>
&lt;str&gt; = path.abspath(&lt;path&gt;) <span class="hljs-comment"># Returns absolute path.</span>
&lt;str&gt; = path.realpath(&lt;path&gt;) <span class="hljs-comment"># Resolves symlinks and calls path.abspath().</span>
</code></pre> </code></pre>
<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> <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>
&lt;str&gt; = path.dirname(&lt;path&gt;) <span class="hljs-comment"># Returns path without the final component.</span> &lt;str&gt; = path.dirname(&lt;path&gt;) <span class="hljs-comment"># Returns path without the final component.</span>
&lt;tup.&gt; = path.splitext(&lt;path&gt;) <span class="hljs-comment"># Splits on last period of the final component.</span> &lt;tup.&gt; = path.splitext(&lt;path&gt;) <span class="hljs-comment"># Splits on last period of the final component.</span>
</code></pre> </code></pre>
<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>
&lt;list&gt; = glob(<span class="hljs-string">'&lt;pattern&gt;'</span>) <span class="hljs-comment"># Returns paths matching the wildcard pattern.</span>
<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>
&lt;list&gt; = glob.glob(<span class="hljs-string">'&lt;pattern&gt;'</span>) <span class="hljs-comment"># Returns paths matching the wildcard pattern.</span>
</code></pre> </code></pre>
<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> <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>
&lt;bool&gt; = path.isfile(&lt;path&gt;) <span class="hljs-comment"># Or: &lt;DirEntry/Path&gt;.is_file()</span> &lt;bool&gt; = path.isfile(&lt;path&gt;) <span class="hljs-comment"># Or: &lt;DirEntry/Path&gt;.is_file()</span>
@ -1404,19 +1404,18 @@ value = args.&lt;name&gt;
<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> <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>
&lt;real&gt; = &lt;stat&gt;.st_mtime/st_size/… <span class="hljs-comment"># Modification time, size in bytes, ...</span> &lt;real&gt; = &lt;stat&gt;.st_mtime/st_size/… <span class="hljs-comment"># Modification time, size in bytes, ...</span>
</code></pre> </code></pre>
<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>
<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>
&lt;str&gt; = &lt;DirEntry&gt;.path <span class="hljs-comment"># Returns the whole path as a string.</span> &lt;str&gt; = &lt;DirEntry&gt;.path <span class="hljs-comment"># Returns the whole path as a string.</span>
&lt;str&gt; = &lt;DirEntry&gt;.name <span class="hljs-comment"># Returns final component as a string.</span> &lt;str&gt; = &lt;DirEntry&gt;.name <span class="hljs-comment"># Returns final component as a string.</span>
&lt;file&gt; = open(&lt;DirEntry&gt;) <span class="hljs-comment"># Opens the file and returns a file object.</span> &lt;file&gt; = open(&lt;DirEntry&gt;) <span class="hljs-comment"># Opens the file and returns a file object.</span>
</code></pre></div> </code></pre></div>
<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
<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>
&lt;Path&gt; = &lt;path&gt; / &lt;path&gt; [/ ...] <span class="hljs-comment"># First or second path must be a Path object.</span>
&lt;Path&gt; = &lt;Path&gt;.resolve() <span class="hljs-comment"># Resolves symlinks and calls &lt;Path&gt;.absolute().</span>
</code></pre></div> </code></pre></div>
<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>
&lt;Path&gt; = &lt;path&gt; / &lt;path&gt; [/ ...] <span class="hljs-comment"># First or second path must be a Path object.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;Path&gt; = Path() <span class="hljs-comment"># Returns relative cwd. Also Path('.').</span> <pre><code class="python language-python hljs">&lt;Path&gt; = Path() <span class="hljs-comment"># Returns relative cwd. Also Path('.').</span>
&lt;Path&gt; = Path.cwd() <span class="hljs-comment"># Returns absolute cwd. Also Path().resolve().</span> &lt;Path&gt; = Path.cwd() <span class="hljs-comment"># Returns absolute cwd. Also Path().resolve().</span>
&lt;Path&gt; = Path.home() <span class="hljs-comment"># Returns user's home directory (absolute).</span> &lt;Path&gt; = Path.home() <span class="hljs-comment"># Returns user's home directory (absolute).</span>
@ -1442,10 +1441,12 @@ os.mkdir(&lt;path&gt;, mode=<span class="hljs-number">0o777</span>) <span
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> 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>
</code></pre> </code></pre>
<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> <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>
shutil.copy2(from, to) <span class="hljs-comment"># Also copies creation and modification time.</span>
shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. 'to' must not exist.</span> shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. 'to' must not exist.</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span> <pre><code class="python language-python hljs">os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span>
os.replace(from, to) <span class="hljs-comment"># Same, but overwrites 'to' if it exists.</span>
os.replace(from, to) <span class="hljs-comment"># Same, but overwrites file 'to' even on Windows.</span>
shutil.move(from, to) <span class="hljs-comment"># Rename() that moves into 'to' if it's a dir.</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">os.remove(&lt;path&gt;) <span class="hljs-comment"># Deletes the file.</span> <pre><code class="python language-python hljs">os.remove(&lt;path&gt;) <span class="hljs-comment"># Deletes the file.</span>
os.rmdir(&lt;path&gt;) <span class="hljs-comment"># Deletes the empty directory.</span> os.rmdir(&lt;path&gt;) <span class="hljs-comment"># Deletes the empty directory.</span>
@ -2934,7 +2935,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer> <footer>
<aside>April 11, 2023</aside>
<aside>April 12, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer> </footer>

4
parse.js

@ -88,11 +88,13 @@ const DATACLASS =
const SHUTIL_COPY = const SHUTIL_COPY =
'shutil.copy(from, to) <span class="hljs-comment"># Copies the file. \'to\' can exist or be a dir.</span>\n' + 'shutil.copy(from, to) <span class="hljs-comment"># Copies the file. \'to\' can exist or be a dir.</span>\n' +
'shutil.copy2(from, to) <span class="hljs-comment"># Also copies creation and modification time.</span>\n' +
'shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. \'to\' must not exist.</span>\n'; 'shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. \'to\' must not exist.</span>\n';
const OS_RENAME = const OS_RENAME =
'os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span>\n' + 'os.rename(from, to) <span class="hljs-comment"># Renames/moves the file or directory.</span>\n' +
'os.replace(from, to) <span class="hljs-comment"># Same, but overwrites \'to\' if it exists.</span>\n';
'os.replace(from, to) <span class="hljs-comment"># Same, but overwrites file \'to\' even on Windows.</span>\n' +
'shutil.move(from, to) <span class="hljs-comment"># Rename() that moves into \'to\' if it\'s a dir.</span>\n';
const STRUCT_FORMAT = const STRUCT_FORMAT =
'<span class="hljs-section">\'&lt;n&gt;s\'</span><span class="hljs-attribute"></span>'; '<span class="hljs-section">\'&lt;n&gt;s\'</span><span class="hljs-attribute"></span>';

Loading…
Cancel
Save