Skip to content

Commit 5075e9a

Browse files
committed
Path
1 parent 0b26977 commit 5075e9a

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ Open
12631263
```python
12641264
<file> = open('<path>', mode='r', encoding=None, endline=None)
12651265
```
1266-
* **`'encoding=None'` means default encoding is used, which is platform dependent. Best practice is to use `"encoding='utf-8'"` whenever possible.**
1266+
* **`'encoding=None'` means default encoding is used, which is platform dependent. Best practice is to use `'encoding="utf-8"'` whenever possible.**
12671267
* **`'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.**
12681268

12691269
### Modes
@@ -1318,21 +1318,26 @@ Path
13181318
----
13191319
```python
13201320
from os import path, listdir
1321+
from glob import glob
1322+
```
1323+
1324+
```python
13211325
<bool> = path.exists('<path>')
13221326
<bool> = path.isfile('<path>')
13231327
<bool> = path.isdir('<path>')
1324-
<list> = listdir('<path>')
13251328
```
13261329

13271330
```python
1328-
>>> from glob import glob
1329-
>>> glob('../*.gif')
1330-
['1.gif', 'card.gif']
1331+
<list> = listdir('<path>') # List of filenames located at 'path'.
1332+
<list> = glob('<pattern>') # Filenames matching the wildcard pattern.
13311333
```
13321334

13331335
### Pathlib
13341336
```python
13351337
from pathlib import Path
1338+
```
1339+
1340+
```python
13361341
cwd = Path()
13371342
<Path> = Path('<path>' [, '<path>', <Path>, ...])
13381343
<Path> = <Path> / '<dir>' / '<file>'
@@ -1342,11 +1347,11 @@ cwd = Path()
13421347
<bool> = <Path>.exists()
13431348
<bool> = <Path>.is_file()
13441349
<bool> = <Path>.is_dir()
1345-
<iter> = <Path>.iterdir()
13461350
```
13471351

13481352
```python
1349-
<iter> = <Path>.glob('<pattern>')
1353+
<iter> = <Path>.iterdir() # Iterator with filenames located at path.
1354+
<iter> = <Path>.glob('<pattern>') # Filenames matching the wildcard pattern.
13501355
```
13511356

13521357
```python

index.html

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ <h2 id="open"><a href="#open" name="open">#</a>Open</h2>
11441144
<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>)
11451145
</code></pre>
11461146
<ul>
1147-
<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>
1147+
<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>
11481148
<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>
11491149
</ul>
11501150
<h3 id="modes">Modes</h3>
@@ -1189,27 +1189,28 @@ <h3 id="writetexttofile">Write Text to File</h3>
11891189
</code></pre>
11901190
<h2 id="path"><a href="#path" name="path">#</a>Path</h2>
11911191
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> os <span class="hljs-keyword">import</span> path, listdir
1192-
&lt;bool&gt; = path.exists(<span class="hljs-string">'&lt;path&gt;'</span>)
1192+
<span class="hljs-keyword">from</span> glob <span class="hljs-keyword">import</span> glob
1193+
</code></pre>
1194+
<pre><code class="python language-python hljs">&lt;bool&gt; = path.exists(<span class="hljs-string">'&lt;path&gt;'</span>)
11931195
&lt;bool&gt; = path.isfile(<span class="hljs-string">'&lt;path&gt;'</span>)
11941196
&lt;bool&gt; = path.isdir(<span class="hljs-string">'&lt;path&gt;'</span>)
1195-
&lt;list&gt; = listdir(<span class="hljs-string">'&lt;path&gt;'</span>)
11961197
</code></pre>
1197-
<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
1198-
<span class="hljs-meta">&gt;&gt;&gt; </span>glob(<span class="hljs-string">'../*.gif'</span>)
1199-
[<span class="hljs-string">'1.gif'</span>, <span class="hljs-string">'card.gif'</span>]
1198+
<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>
1199+
&lt;list&gt; = glob(<span class="hljs-string">'&lt;pattern&gt;'</span>) <span class="hljs-comment"># Filenames matching the wildcard pattern.</span>
12001200
</code></pre>
12011201
<h3 id="pathlib">Pathlib</h3>
12021202
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
1203-
cwd = Path()
1203+
</code></pre>
1204+
<pre><code class="python language-python hljs">cwd = Path()
12041205
&lt;Path&gt; = Path(<span class="hljs-string">'&lt;path&gt;'</span> [, <span class="hljs-string">'&lt;path&gt;'</span>, &lt;Path&gt;, ...])
12051206
&lt;Path&gt; = &lt;Path&gt; / <span class="hljs-string">'&lt;dir&gt;'</span> / <span class="hljs-string">'&lt;file&gt;'</span>
12061207
</code></pre>
12071208
<pre><code class="python language-python hljs">&lt;bool&gt; = &lt;Path&gt;.exists()
12081209
&lt;bool&gt; = &lt;Path&gt;.is_file()
12091210
&lt;bool&gt; = &lt;Path&gt;.is_dir()
1210-
&lt;iter&gt; = &lt;Path&gt;.iterdir()
12111211
</code></pre>
1212-
<pre><code class="python language-python hljs">&lt;iter&gt; = &lt;Path&gt;.glob(<span class="hljs-string">'&lt;pattern&gt;'</span>)
1212+
<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>
1213+
&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>
12131214
</code></pre>
12141215
<pre><code class="python language-python hljs">&lt;str&gt; = str(&lt;Path&gt;) <span class="hljs-comment"># Returns path as a string.</span>
12151216
&lt;tup.&gt; = &lt;Path&gt;.parts <span class="hljs-comment"># Returns all components as strings.</span>

0 commit comments

Comments
 (0)