Browse Source

Path

pull/36/head
Jure Šorn 5 years ago
parent
commit
5075e9a20c
2 changed files with 22 additions and 16 deletions
  1. 19
      README.md
  2. 19
      index.html

19
README.md

@ -1263,7 +1263,7 @@ Open
```python ```python
<file> = open('<path>', mode='r', encoding=None, endline=None) <file> = open('<path>', mode='r', encoding=None, endline=None)
``` ```
* **`'encoding=None'` means default encoding is used, which is platform dependent. Best practice is to use `"encoding='utf-8'"` whenever possible.**
* **`'encoding=None'` means default encoding is used, which is platform dependent. Best practice is to use `'encoding="utf-8"'` whenever possible.**
* **`'endline=None'` means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.** * **`'endline=None'` means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.**
### Modes ### Modes
@ -1318,21 +1318,26 @@ Path
---- ----
```python ```python
from os import path, listdir from os import path, listdir
from glob import glob
```
```python
<bool> = path.exists('<path>') <bool> = path.exists('<path>')
<bool> = path.isfile('<path>') <bool> = path.isfile('<path>')
<bool> = path.isdir('<path>') <bool> = path.isdir('<path>')
<list> = listdir('<path>')
``` ```
```python ```python
>>> from glob import glob
>>> glob('../*.gif')
['1.gif', 'card.gif']
<list> = listdir('<path>') # List of filenames located at 'path'.
<list> = glob('<pattern>') # Filenames matching the wildcard pattern.
``` ```
### Pathlib ### Pathlib
```python ```python
from pathlib import Path from pathlib import Path
```
```python
cwd = Path() cwd = Path()
<Path> = Path('<path>' [, '<path>', <Path>, ...]) <Path> = Path('<path>' [, '<path>', <Path>, ...])
<Path> = <Path> / '<dir>' / '<file>' <Path> = <Path> / '<dir>' / '<file>'
@ -1342,11 +1347,11 @@ cwd = Path()
<bool> = <Path>.exists() <bool> = <Path>.exists()
<bool> = <Path>.is_file() <bool> = <Path>.is_file()
<bool> = <Path>.is_dir() <bool> = <Path>.is_dir()
<iter> = <Path>.iterdir()
``` ```
```python ```python
<iter> = <Path>.glob('<pattern>')
<iter> = <Path>.iterdir() # Iterator with filenames located at path.
<iter> = <Path>.glob('<pattern>') # Filenames matching the wildcard pattern.
``` ```
```python ```python

19
index.html

@ -1144,7 +1144,7 @@ value = args.&lt;name&gt;
<pre><code class="python language-python hljs">&lt;file&gt; = open(<span class="hljs-string">'&lt;path&gt;'</span>, mode=<span class="hljs-string">'r'</span>, encoding=<span class="hljs-keyword">None</span>, endline=<span class="hljs-keyword">None</span>) <pre><code class="python language-python hljs">&lt;file&gt; = open(<span class="hljs-string">'&lt;path&gt;'</span>, mode=<span class="hljs-string">'r'</span>, encoding=<span class="hljs-keyword">None</span>, endline=<span class="hljs-keyword">None</span>)
</code></pre> </code></pre>
<ul> <ul>
<li><strong><code class="python hljs"><span class="hljs-string">'encoding=None'</span></code> means default encoding is used, which is platform dependent. Best practice is to use <code class="python hljs"><span class="hljs-string">"encoding='utf-8'"</span></code> whenever possible.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'encoding=None'</span></code> means default encoding is used, which is platform dependent. Best practice is to use <code class="python hljs"><span class="hljs-string">'encoding="utf-8"'</span></code> whenever possible.</strong></li>
<li><strong><code class="python hljs"><span class="hljs-string">'endline=None'</span></code> means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.</strong></li> <li><strong><code class="python hljs"><span class="hljs-string">'endline=None'</span></code> means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.</strong></li>
</ul> </ul>
<h3 id="modes">Modes</h3> <h3 id="modes">Modes</h3>
@ -1189,27 +1189,28 @@ value = args.&lt;name&gt;
</code></pre> </code></pre>
<h2 id="path"><a href="#path" name="path">#</a>Path</h2> <h2 id="path"><a href="#path" name="path">#</a>Path</h2>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> os <span class="hljs-keyword">import</span> path, listdir <pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> os <span class="hljs-keyword">import</span> path, listdir
&lt;bool&gt; = path.exists(<span class="hljs-string">'&lt;path&gt;'</span>)
<span class="hljs-keyword">from</span> glob <span class="hljs-keyword">import</span> glob
</code></pre>
<pre><code class="python language-python hljs">&lt;bool&gt; = path.exists(<span class="hljs-string">'&lt;path&gt;'</span>)
&lt;bool&gt; = path.isfile(<span class="hljs-string">'&lt;path&gt;'</span>) &lt;bool&gt; = path.isfile(<span class="hljs-string">'&lt;path&gt;'</span>)
&lt;bool&gt; = path.isdir(<span class="hljs-string">'&lt;path&gt;'</span>) &lt;bool&gt; = path.isdir(<span class="hljs-string">'&lt;path&gt;'</span>)
&lt;list&gt; = listdir(<span class="hljs-string">'&lt;path&gt;'</span>)
</code></pre> </code></pre>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> glob <span class="hljs-keyword">import</span> glob
<span class="hljs-meta">&gt;&gt;&gt; </span>glob(<span class="hljs-string">'../*.gif'</span>)
[<span class="hljs-string">'1.gif'</span>, <span class="hljs-string">'card.gif'</span>]
<pre><code class="python language-python hljs">&lt;list&gt; = listdir(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># List of filenames located at 'path'. </span>
&lt;list&gt; = glob(<span class="hljs-string">'&lt;pattern&gt;'</span>) <span class="hljs-comment"># Filenames matching the wildcard pattern.</span>
</code></pre> </code></pre>
<h3 id="pathlib">Pathlib</h3> <h3 id="pathlib">Pathlib</h3>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path <pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
cwd = Path()
</code></pre>
<pre><code class="python language-python hljs">cwd = Path()
&lt;Path&gt; = Path(<span class="hljs-string">'&lt;path&gt;'</span> [, <span class="hljs-string">'&lt;path&gt;'</span>, &lt;Path&gt;, ...]) &lt;Path&gt; = Path(<span class="hljs-string">'&lt;path&gt;'</span> [, <span class="hljs-string">'&lt;path&gt;'</span>, &lt;Path&gt;, ...])
&lt;Path&gt; = &lt;Path&gt; / <span class="hljs-string">'&lt;dir&gt;'</span> / <span class="hljs-string">'&lt;file&gt;'</span> &lt;Path&gt; = &lt;Path&gt; / <span class="hljs-string">'&lt;dir&gt;'</span> / <span class="hljs-string">'&lt;file&gt;'</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;bool&gt; = &lt;Path&gt;.exists() <pre><code class="python language-python hljs">&lt;bool&gt; = &lt;Path&gt;.exists()
&lt;bool&gt; = &lt;Path&gt;.is_file() &lt;bool&gt; = &lt;Path&gt;.is_file()
&lt;bool&gt; = &lt;Path&gt;.is_dir() &lt;bool&gt; = &lt;Path&gt;.is_dir()
&lt;iter&gt; = &lt;Path&gt;.iterdir()
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;iter&gt; = &lt;Path&gt;.glob(<span class="hljs-string">'&lt;pattern&gt;'</span>)
<pre><code class="python language-python hljs">&lt;iter&gt; = &lt;Path&gt;.iterdir() <span class="hljs-comment"># Iterator with filenames located at path.</span>
&lt;iter&gt; = &lt;Path&gt;.glob(<span class="hljs-string">'&lt;pattern&gt;'</span>) <span class="hljs-comment"># Filenames matching the wildcard pattern.</span>
</code></pre> </code></pre>
<pre><code class="python language-python hljs">&lt;str&gt; = str(&lt;Path&gt;) <span class="hljs-comment"># Returns path as a string.</span> <pre><code class="python language-python hljs">&lt;str&gt; = str(&lt;Path&gt;) <span class="hljs-comment"># Returns path as a string.</span>
&lt;tup.&gt; = &lt;Path&gt;.parts <span class="hljs-comment"># Returns all components as strings.</span> &lt;tup.&gt; = &lt;Path&gt;.parts <span class="hljs-comment"># Returns all components as strings.</span>

Loading…
Cancel
Save