|
1114 | 1114 | <span class="hljs-meta">... </span> print(file.read())
|
1115 | 1115 | Hello World!
|
1116 | 1116 | </code></pre>
|
1117 |
| -<div><h4 id="listofexistingcontextmanagers">List of existing context managers:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> open(<span class="hljs-string">'<path>'</span>) <span class="hljs-keyword">as</span> file: ... |
| 1117 | +<div><h4 id="listofcoveredcontextmanagers">List of covered context managers:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> open(<span class="hljs-string">'<path>'</span>) <span class="hljs-keyword">as</span> file: ... |
1118 | 1118 | <span class="hljs-keyword">with</span> wave.open(<span class="hljs-string">'<path>'</span>) <span class="hljs-keyword">as</span> wave_file: ...
|
1119 | 1119 | <span class="hljs-keyword">with</span> memoryview(<bytes/bytearray/array>) <span class="hljs-keyword">as</span> view: ...
|
1120 | 1120 | <span class="hljs-keyword">with</span> concurrent.futures.ThreadPoolExecutor() <span class="hljs-keyword">as</span> executor: ...
|
|
1470 | 1470 | <li><strong>Paths can be either strings, Paths, or DirEntry objects.</strong></li>
|
1471 | 1471 | <li><strong>Functions report OS related errors by raising either OSError or one of its <a href="#exceptions-1">subclasses</a>.</strong></li>
|
1472 | 1472 | </ul><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> os, shutil
|
1473 |
| -<str> = os.getcwd() <span class="hljs-comment"># Returns the current working directory.</span> |
1474 |
| -os.chdir(<path>) <span class="hljs-comment"># Changes current working directory.</span> |
1475 | 1473 | </code></pre></div></div>
|
1476 | 1474 |
|
1477 | 1475 |
|
1478 | 1476 |
|
1479 |
| -<pre><code class="python language-python hljs">shutil.copy(from, to) <span class="hljs-comment"># Copies the file.</span> |
1480 |
| -os.rename(from, to) <span class="hljs-comment"># Renames the file or directory.</span> |
| 1477 | +<pre><code class="python language-python hljs">os.chdir(<path>) <span class="hljs-comment"># Changes current working directory.</span> |
| 1478 | +os.mkdir(<path>, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates a directory.</span> |
| 1479 | +</code></pre> |
| 1480 | +<pre><code class="python language-python hljs">os.rename(from, to) <span class="hljs-comment"># Renames the file or directory.</span> |
1481 | 1481 | os.replace(from, to) <span class="hljs-comment"># Same, but overwrites 'to' if it exists.</span>
|
1482 | 1482 | </code></pre>
|
1483 | 1483 | <pre><code class="python language-python hljs">os.remove(<path>) <span class="hljs-comment"># Deletes the file.</span>
|
1484 | 1484 | os.rmdir(<path>) <span class="hljs-comment"># Deletes empty directory.</span>
|
1485 | 1485 | shutil.rmtree(<path>) <span class="hljs-comment"># Deletes the entire directory tree.</span>
|
1486 | 1486 | </code></pre>
|
1487 |
| -<pre><code class="python language-python hljs">os.mkdir(<path>, mode=<span class="hljs-number">0o777</span>) <span class="hljs-comment"># Creates a directory.</span> |
| 1487 | +<pre><code class="python language-python hljs">shutil.copy(from, to) <span class="hljs-comment"># Copies the file.</span> |
| 1488 | +shutil.copytree(from, to) <span class="hljs-comment"># Copies the entire directory tree.</span> |
| 1489 | +</code></pre> |
| 1490 | +<pre><code class="python language-python hljs"><str> = os.getcwd() <span class="hljs-comment"># Returns the current working directory.</span> |
1488 | 1491 | <iter> = os.scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns os.DirEntry objects located at path.</span>
|
1489 | 1492 | </code></pre>
|
1490 | 1493 | <div><h4 id="direntry">DirEntry:</h4><pre><code class="python language-python hljs"><bool> = <DirEntry>.is_file()
|
|
0 commit comments