Skip to content

Commit 09e2d9c

Browse files
author
giumas
committed
updated authors and readme
1 parent 64ff45f commit 09e2d9c

7 files changed

+597
-336
lines changed

006_Read_and_Write_Text_Files.ipynb

+112-29
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
"cell_type": "markdown",
2020
"metadata": {},
2121
"source": [
22-
"You have learned about lists as well as how to write your own functions with loops and conditional statements. As such, you can already write programs performing a variety of tasks. \n",
22+
"You have learned about lists as well, and how to write your own functions with loops and conditional statements. This allows you to write programs performing a variety of tasks. \n",
2323
"\n",
24-
"However, something that you are currently missing is a mechanism to access the data that you want to analyze. A very common way to access these data is through (local or remotely-stored) [files](https://en.wikipedia.org/wiki/Computer_file)."
24+
"However, a convenient mechanism to access the data that you want to analyze is currently missing. In this notebook, we will explore the use of [files](https://en.wikipedia.org/wiki/Computer_file) since they are a common way to access stored data."
2525
]
2626
},
2727
{
@@ -30,7 +30,7 @@
3030
"source": [
3131
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
3232
"\n",
33-
"A **file** provides a mechanism for **permanently storing information** so that they can be retrieved when your program and/or your machine are restarted."
33+
"A **file** provides a mechanism for **permanently storing information**. Thus, the file content is not lost in the event of a [crash](https://en.wikipedia.org/wiki/Crash_(computing)) or [reboot](https://en.wikipedia.org/wiki/Reboot)."
3434
]
3535
},
3636
{
@@ -55,14 +55,14 @@
5555
"source": [
5656
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
5757
"\n",
58-
"A **binary file** is any other type of file that does not fit the previous definition of text file."
58+
"A **binary file** is any other type of file that does not fit the previous definition of a text file."
5959
]
6060
},
6161
{
6262
"cell_type": "markdown",
6363
"metadata": {},
6464
"source": [
65-
"You can often recognize a text files by looking at the [file extension](https://en.wikipedia.org/wiki/Filename_extension). Extensions commonly in use for text files are: `.txt`, `.asc`, `.xyz`."
65+
"You can often recognize a text file by looking at the [file extension](https://en.wikipedia.org/wiki/Filename_extension). Extensions commonly in use for text files are: `.txt`, `.asc`, `.xyz`."
6666
]
6767
},
6868
{
@@ -71,14 +71,14 @@
7171
"source": [
7272
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/info.png\">\n",
7373
"\n",
74-
"A very simple test to evaluate whether a given file is a text file is to open it in a text editor. If you can understand the visualized content of an opened file, then the file is likely a text file. *(Be warned that opening a file in this way can take a long time depending on the size of the file.)*"
74+
"A very simple test to evaluate whether a given file is a text file is to open it in a text editor. If you can recognize the visualized content of an opened file as text, then the file is likely a text file. *(Be warned that opening a file in this way can take a long time depending on the size of the file.)*"
7575
]
7676
},
7777
{
7878
"cell_type": "markdown",
7979
"metadata": {},
8080
"source": [
81-
"We will first introduce some file managing capability of the `os` [Python module](https://docs.python.org/3.6/tutorial/modules.html#modules), then we will describe the use of the functions that Python provides for [reading and writing the content of a text file](https://docs.python.org/3.6/tutorial/inputoutput.html)."
81+
"We will first introduce some file managing capabilities of the `os.path` [Python module](https://docs.python.org/3.6/tutorial/modules.html#modules), then we will use the functions that Python provides for [reading and writing the content of a text file](https://docs.python.org/3.6/tutorial/inputoutput.html)."
8282
]
8383
},
8484
{
@@ -87,7 +87,16 @@
8787
"source": [
8888
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
8989
"\n",
90-
"In Python, a **module** is a file containing definitions and statements. The module name is given by the file name without the suffix `.py`."
90+
"In Python, a **module** is a file containing definitions and statements. "
91+
]
92+
},
93+
{
94+
"cell_type": "markdown",
95+
"metadata": {},
96+
"source": [
97+
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/info.png\">\n",
98+
"\n",
99+
"The module name is given by the file name without the [file extension](https://en.wikipedia.org/wiki/Filename_extension). For example, a file `example.py` may identify the module `example`."
91100
]
92101
},
93102
{
@@ -103,26 +112,59 @@
103112
"cell_type": "markdown",
104113
"metadata": {},
105114
"source": [
106-
"## The `os` module"
115+
"## The `os.path` module"
107116
]
108117
},
109118
{
110119
"cell_type": "markdown",
111120
"metadata": {},
112121
"source": [
113-
"The `os` module provides a **portable** way of using several functionalities [across different operating systems](https://en.wikipedia.org/wiki/Cross-platform_software) (i.e., the same code can run on [Linux Ubuntu](https://en.wikipedia.org/wiki/Ubuntu) and [Microsoft Windows 10](https://en.wikipedia.org/wiki/Windows_10))."
122+
"We will explore the `os.path` module to retrieve some data files that are stored on the server's hard disk."
114123
]
115124
},
116125
{
117126
"cell_type": "markdown",
118127
"metadata": {},
119128
"source": [
120-
"In particular, we will explore the `os.path` sub-module to retrieve some data files that are stored on the server's hard disk.\n",
129+
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/info.png\">\n",
121130
"\n",
122-
"The first required operation is to **import** the `os` module. Then, we will use some of the `os.path` sub-module functionalities and variables to write a function that returns the full path of the folder where this notebook is located:\n",
131+
"Several functionalities in the `os.path` module are **portable**. This means that can be used [across different operating systems](https://en.wikipedia.org/wiki/Cross-platform_software). For example, you can use its functionalities in code that runs on [Linux Ubuntu](https://en.wikipedia.org/wiki/Ubuntu) and [Microsoft Windows 10](https://en.wikipedia.org/wiki/Windows_10)."
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {},
137+
"source": [
138+
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
139+
"\n",
140+
"For using the `os.path` module, the first required operation is to **import** it. Once imported, the module can be used."
141+
]
142+
},
143+
{
144+
"cell_type": "markdown",
145+
"metadata": {},
146+
"source": [
147+
"In the example below, we write a `get_current_folder()` function that returns the path of the folder where this notebook is located."
148+
]
149+
},
150+
{
151+
"cell_type": "markdown",
152+
"metadata": {},
153+
"source": [
154+
"To achieve this task, we will use two of the `os.path` functionalities and variables:\n",
155+
"\n",
156+
"- `curdir`: The string used by the [operating system](https://en.wikipedia.org/wiki/Operating_system) to refer to the current directory.\n",
157+
"- `abspath()`: A function that returns the [absolute path](https://en.wikipedia.org/wiki/Path_(computing)#Absolute_and_relative_paths)."
158+
]
159+
},
160+
{
161+
"attachments": {},
162+
"cell_type": "markdown",
163+
"metadata": {},
164+
"source": [
165+
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/info.png\">\n",
123166
"\n",
124-
"- `curdir`: The constant string used by the operating system to refer to the current directory. E.g., `.` for Windows and Linux.\n",
125-
"- `abspath()`: A function that returns the full, absolute version of a path."
167+
"An [**absolute path**](https://en.wikipedia.org/wiki/Path_(computing)#Absolute_and_relative_paths) points to the same location in a file system, regardless of the current working directory. In contrast, a [**relative path**](https://en.wikipedia.org/wiki/Path_(computing)#Absolute_and_relative_paths) starts from a given working directory."
126168
]
127169
},
128170
{
@@ -131,7 +173,7 @@
131173
"metadata": {},
132174
"outputs": [],
133175
"source": [
134-
"import os\n",
176+
"import os.path\n",
135177
"\n",
136178
"def get_current_folder():\n",
137179
" cur_folder = os.path.abspath(os.path.curdir)\n",
@@ -153,9 +195,9 @@
153195
"cell_type": "markdown",
154196
"metadata": {},
155197
"source": [
156-
"As such, we extend the previous code using `os.path.join()` and `os.path.exist()` functions to:\n",
198+
"To be able to access the `data` sub-folder, we extend the previous code using `os.path.join()` and `os.path.exist()` functions to:\n",
157199
"\n",
158-
"- Create the full path to the `data` sub-folder.\n",
200+
"- Create the absolute path to the `data` sub-folder.\n",
159201
"- Check whether the resulting path actually exists."
160202
]
161203
},
@@ -189,7 +231,14 @@
189231
"source": [
190232
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
191233
"\n",
192-
"We did not import the `os` module since it was already imported in the previous cell. Re-importing a module does not break your code, but makes it more verbose. "
234+
"We did not import the `os.path` module since it was already imported in the previous code cell. Re-importing a module does not break your code, but makes it more verbose. "
235+
]
236+
},
237+
{
238+
"cell_type": "markdown",
239+
"metadata": {},
240+
"source": [
241+
"However, if you decide to [clear the results of this notebook](000_Welcome_on_Board.ipynb#How-to-Clear-the-Results-of-a-Notebook?), you will need to re-execute the code cell with the `import` statement."
193242
]
194243
},
195244
{
@@ -298,7 +347,7 @@
298347
"cell_type": "markdown",
299348
"metadata": {},
300349
"source": [
301-
"As discussed above, a text file is a sequence of characters stored on a permanent medium (e.g., a flash memory)."
350+
"As discussed above, a text file is a sequence of characters stored on a permanent medium (e.g., a [USB flash drive](https://en.wikipedia.org/wiki/USB_flash_drive))."
302351
]
303352
},
304353
{
@@ -388,16 +437,30 @@
388437
"cell_type": "markdown",
389438
"metadata": {},
390439
"source": [
391-
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/info.png\">\n",
440+
"Why the characters are 100? There are 20 rows in the file. Each row has 4 visible characters (e.g., `30.8`) but there is also an invisible [newline character](https://en.wikipedia.org/wiki/Newline) (i.e., `\\n`) that text editors interpret as a new line. Thus, `(4+1) * 20 = 100` characters."
441+
]
442+
},
443+
{
444+
"cell_type": "markdown",
445+
"metadata": {},
446+
"source": [
447+
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n",
392448
"\n",
393-
"Why the characters are 100? Each row has 4 visible characters (e.g., `30.8`) but there is also an invisible character (i.e., `\\n`) that the text editor interprets as a new line. Thus, `(4+1) * 20 = 100` characters."
449+
"The **newline character** is used to control the end of a line of text and the start of a new one."
450+
]
451+
},
452+
{
453+
"cell_type": "markdown",
454+
"metadata": {},
455+
"source": [
456+
"In the code above, the `sal_content` variable holds the content of the file as a single sequence of characters."
394457
]
395458
},
396459
{
397460
"cell_type": "markdown",
398461
"metadata": {},
399462
"source": [
400-
"We will now write a function that not only reads the sequence of characters, but also splits them by line (using the `str` method named `splitlines()`) and converts the result in the corresponding `float` value."
463+
"We will now write a function that not only reads the sequence of characters, but also splits them in multiple lines based on the **newline character** (using the `str` method named `splitlines()`). Finally, we convert the result in the corresponding `float` value and append this value to `sal_list`."
401464
]
402465
},
403466
{
@@ -503,7 +566,7 @@
503566
"cell_type": "markdown",
504567
"metadata": {},
505568
"source": [
506-
"If you want to write a text file, the first decision to take is the location on where to store the text file. For this collection of notebook, we will use the `output` sub-folder that can be retrieved running the following code:"
569+
"If you want to write a text file, you need to decide where to store it. For this collection of notebooks, we will use the `output` sub-folder that can be retrieved running the following code:"
507570
]
508571
},
509572
{
@@ -513,8 +576,8 @@
513576
"outputs": [],
514577
"source": [
515578
"def get_output_folder():\n",
516-
" cur_folder = os.path.abspath(os.path.curdir)\n",
517-
" output_folder = os.path.join(cur_folder, \"output\")\n",
579+
" cur_folder = os.path.abspath(os.path.curdir) # The absolute path to the current directory\n",
580+
" output_folder = os.path.join(cur_folder, \"output\") # The absolute path to the output folder (may or may not exist)\n",
518581
" if os.path.exists(output_folder):\n",
519582
" return output_folder\n",
520583
" else: # in case that the output folder does not exists, we raise a meaningful error\n",
@@ -545,7 +608,26 @@
545608
"cell_type": "markdown",
546609
"metadata": {},
547610
"source": [
548-
"To write a file, you have to use the `open()` function and pass the `w` mode (`w` is for *write*) as second parameter. We put this function within a function that take a list as a second parameter and write the content into the text file."
611+
"In the code below, the `write_list_to_disk` function takes:\n",
612+
"\n",
613+
"* An `output_path` where the output file is to be written. \n",
614+
"* A `input_list` containing the data to be written in the output file."
615+
]
616+
},
617+
{
618+
"cell_type": "markdown",
619+
"metadata": {},
620+
"source": [
621+
"The below function uses the `open()` function and passes the `w` mode (`w` is for *write*) as second parameter. "
622+
]
623+
},
624+
{
625+
"cell_type": "markdown",
626+
"metadata": {},
627+
"source": [
628+
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/info.png\">\n",
629+
"\n",
630+
"You may learn about other modes for opening a file from the official [Python documentation](https://docs.python.org/3.6/library/functions.html?#open)."
549631
]
550632
},
551633
{
@@ -556,10 +638,10 @@
556638
"source": [
557639
"def write_list_to_disk(output_path, input_list):\n",
558640
" \n",
559-
" output_file = open(output_path, mode=\"w\")\n",
641+
" output_file = open(output_path, mode=\"w\") # mode=\"w\" to open the file in writing mode\n",
560642
" \n",
561643
" for value in input_list:\n",
562-
" line_content = str(value) + \"\\n\" # the \"\\n\" is the 'escaped' character for the new line\n",
644+
" line_content = str(value) + \"\\n\" # the \"\\n\" is the newline character\n",
563645
" output_file.write(line_content)\n",
564646
" \n",
565647
" output_file.close()\n",
@@ -669,7 +751,8 @@
669751
"* [Computer file](https://en.wikipedia.org/wiki/Computer_file)\n",
670752
" * [Text file](https://en.wikipedia.org/wiki/Text_file)\n",
671753
" * [Binary file](https://en.wikipedia.org/wiki/Binary_file)\n",
672-
" * [Filename extension](https://en.wikipedia.org/wiki/Filename_extension)"
754+
" * [Filename extension](https://en.wikipedia.org/wiki/Filename_extension)\n",
755+
"* [Absolute and relative paths](https://en.wikipedia.org/wiki/Path_(computing)#Absolute_and_relative_paths)"
673756
]
674757
},
675758
{

AUTHORS.rst

+18
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,31 @@ ePOM - Programming Basics with Python is developed and maintained by:
1212

1313
- `Semme Dijkstra <mailto:[email protected]>`_
1414

15+
- `Jordan Chadwick <mailto:[email protected]>`_
16+
1517
Contributors
1618
~~~~~~~~~~~~
1719

1820
The following wonderful people contributed directly or indirectly to this project:
1921

22+
- `Anne Hartwell <mailto:[email protected]>`_
23+
24+
- `Brandon Maingot <mailto:[email protected]>`_
25+
26+
- `Christos Kastrisios <mailto:[email protected]>`_
27+
28+
- `Dan Tauriello <mailto:[email protected]>`_
29+
2030
- `Jason Greenlaw <mailto:[email protected]>`_
2131

32+
- `Jeff Douglas <mailto:[email protected]>`_
33+
34+
- `Jenn Dijkstra <mailto:[email protected]>`_
35+
36+
- `Mike Smith <mailto:[email protected]>`_
37+
38+
- `Tomer Ketter <mailto:[email protected]>`_
39+
2240
- `Val Schmidt <mailto:[email protected]>`_
2341

2442

README.rst

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ Programming Basics with Python
33

44
* Code: `GitHub repo <https://github.com/hydroffice/python_basics>`_
55
* Project page: `url <https://www.hydroffice.org/epom>`_
6-
* Code License: `LGPLv3 <https://github.com/hydroffice/python_basics/raw/master/LICENSE>`_
7-
* Text License: `Attribution-NonCommercial-NoDerivs 3.0 Unported <https://github.com/hydroffice/python_basics/raw/master/TEXT>`_
8-
96

107
General Info
118
------------
@@ -17,6 +14,21 @@ This repository contains a collection of Programming Basics with Python notebook
1714

1815
The overall task is to drive you through some basic concepts of programming using the Python language.
1916

17+
18+
Code License
19+
------------
20+
21+
.. image:: https://github.com/hydroffice/python_basics/raw/master/images/LGPLv3.png
22+
:alt: LGPLv3
23+
:target: https://github.com/hydroffice/python_basics/raw/master/LICENSE
24+
25+
Text License
26+
------------
27+
28+
.. image:: https://github.com/hydroffice/python_basics/raw/master/images/CC-BY-SA.png
29+
:alt: CC BY-SA
30+
:target: https://github.com/hydroffice/python_basics/raw/master/TEXT
31+
2032
Copyright Notice
2133
----------------
2234

0 commit comments

Comments
 (0)