Browse Source

Paths, OS Commands

pull/135/head
Jure Šorn 2 years ago
parent
commit
ebad00587a
3 changed files with 34 additions and 34 deletions
  1. 28
      README.md
  2. 32
      index.html
  3. 8
      parse.js

28
README.md

@ -1647,7 +1647,7 @@ from glob import glob
```
### DirEntry
**Using scandir() instead of listdir() can significantly increase the performance of code that also needs file type information.**
**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
<iter> = scandir(path='.') # Returns DirEntry objects located at path.
@ -1703,32 +1703,32 @@ import os, shutil, subprocess
* **Functions report OS related errors by raising either OSError or one of its [subclasses](#exceptions-1).**
```python
os.chdir(<path>) # Changes the current working directory.
os.mkdir(<path>, mode=0o777) # Creates a directory. Mode is in octal.
os.makedirs(<path>, mode=0o777) # Creates all directories in the path.
os.chdir(<path>) # Changes the current working directory.
os.mkdir(<path>, mode=0o777) # Creates a directory. Mode is in octal.
os.makedirs(<path>, mode=0o777) # Creates dirs in path. Also: `exist_ok=False`.
```
```python
shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir.
shutil.copytree(from, to) # Copies the directory. 'to' must not exist.
shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir.
shutil.copytree(from, to) # Copies the directory. 'to' must not exist.
```
```python
os.rename(from, to) # Renames/moves the file or directory.
os.replace(from, to) # Same, but overwrites 'to' if it exists.
os.rename(from, to) # Renames/moves the file or directory.
os.replace(from, to) # Same, but overwrites 'to' if it exists.
```
```python
os.remove(<path>) # Deletes the file.
os.rmdir(<path>) # Deletes the empty directory.
shutil.rmtree(<path>) # Deletes the directory.
os.remove(<path>) # Deletes the file.
os.rmdir(<path>) # Deletes the empty directory.
shutil.rmtree(<path>) # Deletes the directory.
```
### Shell Commands
```python
<pipe> = os.popen('<command>') # Executes command in sh/cmd and returns its stdout pipe.
<str> = <pipe>.read(size=-1) # Reads 'size' chars or until EOF. Also readline/s().
<int> = <pipe>.close() # Closes the pipe. Returns None on success, int on error.
<pipe> = os.popen('<command>') # Executes command in sh/cmd and returns its stdout pipe.
<str> = <pipe>.read(size=-1) # Reads 'size' chars or until EOF. Also readline/s().
<int> = <pipe>.close() # Closes the pipe. Returns None on success, int on error.
```
#### Sends '1 + 1' to the basic calculator and captures its output:

32
index.html

@ -54,7 +54,7 @@
<body>
<header>
<aside>May 23, 2022</aside>
<aside>May 28, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -1400,7 +1400,7 @@ 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>
&lt;real&gt; = &lt;stat&gt;.st_mtime/st_size/… <span class="hljs-comment"># Modification time, size in bytes, …</span>
</code></pre>
<div><h3 id="direntry">DirEntry</h3><p><strong>Using scandir() instead of listdir() can significantly increase the performance of code that also needs file type information.</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 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; = scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns DirEntry objects located at path.</span>
&lt;str&gt; = &lt;DirEntry&gt;.path <span class="hljs-comment"># Returns 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;file&gt; = open(&lt;DirEntry&gt;) <span class="hljs-comment"># Opens the file and returns a file object.</span>
@ -1436,25 +1436,25 @@ value = args.&lt;name&gt;
<div><h3 id="filesanddirectories">Files and Directories</h3><ul>
<li><strong>Paths can be either strings, Paths or DirEntry objects.</strong></li>
<li><strong>Functions report OS related errors by raising either OSError or one of its <a href="#exceptions-1">subclasses</a>.</strong></li>
</ul><pre><code class="python language-python hljs">os.chdir(&lt;path&gt;) <span class="hljs-comment"># Changes the current working directory.</span>
os.mkdir(&lt;path&gt;, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates a directory. Mode is in octal.</span>
os.makedirs(&lt;path&gt;, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates all directories in the path.</span>
</ul><pre><code class="python language-python hljs">os.chdir(&lt;path&gt;) <span class="hljs-comment"># Changes the current working directory.</span>
os.mkdir(&lt;path&gt;, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates a directory. Mode is in octal.</span>
os.makedirs(&lt;path&gt;, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates dirs in path. Also: `exist_ok=False`.</span>
</code></pre></div>
<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.copytree(from, to) <span class="hljs-comment"># Copies the directory. 'to' must not exist.</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.copytree(from, to) <span class="hljs-comment"># Copies the directory. 'to' must not exist.</span>
</code></pre>
<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>
<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>
</code></pre>
<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>
shutil.rmtree(&lt;path&gt;) <span class="hljs-comment"># Deletes the directory.</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>
shutil.rmtree(&lt;path&gt;) <span class="hljs-comment"># Deletes the directory.</span>
</code></pre>
<div><h3 id="shellcommands">Shell Commands</h3><pre><code class="python language-python hljs">&lt;pipe&gt; = os.popen(<span class="hljs-string">'&lt;command&gt;'</span>) <span class="hljs-comment"># Executes command in sh/cmd and returns its stdout pipe.</span>
&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>
&lt;int&gt; = &lt;pipe&gt;.close() <span class="hljs-comment"># Closes the pipe. Returns None on success, int on error.</span>
<div><h3 id="shellcommands">Shell Commands</h3><pre><code class="python language-python hljs">&lt;pipe&gt; = os.popen(<span class="hljs-string">'&lt;command&gt;'</span>) <span class="hljs-comment"># Executes command in sh/cmd and returns its stdout pipe.</span>
&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>
&lt;int&gt; = &lt;pipe&gt;.close() <span class="hljs-comment"># Closes the pipe. Returns None on success, int on error.</span>
</code></pre></div>
<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>)
@ -2896,7 +2896,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer>
<aside>May 23, 2022</aside>
<aside>May 28, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

8
parse.js

@ -69,12 +69,12 @@ const DATACLASS =
'&lt;tuple&gt; = (<span class="hljs-string">\'&lt;attr_name&gt;\'</span>, &lt;type&gt; [, &lt;default_value&gt;])';
const SHUTIL_COPY =
'shutil.copy(from, to) <span class="hljs-comment"># Copies the file. \'to\' can exist or be a dir.</span>\n' +
'shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. \'to\' must not exist.</span>\n';
'shutil.copy(from, to) <span class="hljs-comment"># Copies the file. \'to\' can exist or be a dir.</span>\n' +
'shutil.copytree(from, to) <span class="hljs-comment"># Copies the directory. \'to\' must not exist.</span>\n';
const OS_RENAME =
'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.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';
const TYPE =
'&lt;class&gt; = type(<span class="hljs-string">\'&lt;class_name&gt;\'</span>, &lt;tuple_of_parents&gt;, &lt;dict_of_class_attributes&gt;)';

Loading…
Cancel
Save