You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/notes/dev-tools/google-colab/pip.ipynb
+11-5Lines changed: 11 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -16,11 +16,7 @@
16
16
"\n",
17
17
"But it is very common to want to use other packages in the Python ecosystem as well, including any packages we might find shared on [GitHub](https://github.com/), or hosted more officially on the Python Package Index ([PyPI](https://pypi.org/)), which is a centralized repository for all official Python packages.\n",
18
18
"\n",
19
-
"To manage the Python package installation process, we use Python's sidekick, a command-line tool called [Pip](https://packaging.python.org/en/latest/tutorials/installing-packages/#use-pip-for-installing).\n",
20
-
"\n",
21
-
":::{.callout-note}\n",
22
-
"In Google Colab, when we use terminal commands (such as `pip` commands), we prefix them with an exclamation point (!), to differentiate them from Python code.\n",
23
-
":::\n"
19
+
"To manage the Python package installation process, we use Python's sidekick, a command-line tool called [Pip](https://packaging.python.org/en/latest/tutorials/installing-packages/#use-pip-for-installing).\n"
24
20
]
25
21
},
26
22
{
@@ -46,6 +42,16 @@
46
42
"!pip --version"
47
43
]
48
44
},
45
+
{
46
+
"cell_type": "markdown",
47
+
"metadata": {},
48
+
"source": [
49
+
"\n",
50
+
":::{.callout-note}\n",
51
+
"In Google Colab, when we use terminal commands (such as `pip` commands), we prefix them with an exclamation point (!), to differentiate them from Python code.\n",
Copy file name to clipboardExpand all lines: docs/notes/python-lang/file-operations.qmd
+8-6Lines changed: 8 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -25,18 +25,21 @@ We can use the `open` function in "writing" mode (`"w"`) to write text contents
25
25
```{python}
26
26
filepath = "my_message.txt"
27
27
28
-
# "w" means "open the file for writing":
29
28
with open(filepath, "w") as file:
30
29
file.write(message)
31
30
```
32
31
33
-
Let's break this down. As the first parameter we first specify the name (or the full path) of the file we want to reference.
32
+
Let's break this down.
34
33
35
-
Then as the second parameter specifies the "mode", or the way in which we intend to open the file (i.e. reading or writing).
34
+
When using the `open` function, we pass the name (or the full path) of the file as the first parameter, then the "mode", or the way in which we intend to open the file (i.e. reading or writing), as the second parameter.
36
35
36
+
:::{.callout-note}
37
37
FYI: the `with` statement creates a **context manager** that allows us to open the file without explicitly closing it. The file will be closed when the indentation level resets back to the left margin.
38
+
:::
38
39
39
-
FYI: here, `file` is an alias variable referencing the file object (technically a `TextIOWrapper` datatype). You can choose any variable name you like instead, just reference that same variable name inside the scope of the context manager when reading with the file's `read` method, or writing with the file's `write` method.
40
+
:::{.callout-note}
41
+
Here, `file` is an alias variable referencing the file object (technically a `TextIOWrapper` datatype). You can choose any variable name you like instead, just reference that same variable name inside the scope of the context manager when reading with the file's `read` method, or writing with the file's `write` method.
42
+
:::
40
43
41
44
## Reading Files
42
45
@@ -45,15 +48,14 @@ To verify the contents got written to file, let's read the same file.
45
48
Here we are use the `open` function in "reader" mode (`"r"`) to read the contents of the text file:
46
49
47
50
```{python}
48
-
# "r" means "open the file for reading":
49
51
with open(filepath, "r") as file:
50
52
contents = file.read()
51
53
print(contents)
52
54
```
53
55
54
56
## Removing Files
55
57
56
-
We can use the `os` module to detect, and delete files:
58
+
Finally, we can use the `os` module to detect, and delete files:
0 commit comments