Skip to content

Commit f79fe09

Browse files
committed
Made all Python code interactive.
1 parent 6311e14 commit f79fe09

1 file changed

Lines changed: 25 additions & 25 deletions

File tree

‎source/ch_x_filehandling.ptx‎

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<title>Class Imports</title>
1616

1717
<p>
18-
Java has several libraries included for file handling, though, they must be imported. Java includes a class called File in the io library. The class can be imported with the following line. Be sure to capitalize File.
18+
Java has several libraries included for file handling, though, they must be imported. Java includes a class called <c>File</c> in the <c>io</c> library. The class can be imported with the following line. Be sure to capitalize <c>File</c>.
1919
</p>
2020

2121
<program interactive="activecode" language="java">
@@ -25,7 +25,7 @@
2525
</program>
2626

2727
<p>
28-
The Scanner class from the util library will need to be imported if there is any need for a program to read a file. It should be noted that this library is unnecessary if the program will not be reading any data from a file.
28+
The <c>Scanner</c> class from the <c>util</c> library will need to be imported if there is any need for a program to read a file. It should be noted that this library is unnecessary if the program will not be reading any data from a file.
2929
</p>
3030

3131
<program interactive="activecode" language="java">
@@ -35,7 +35,7 @@
3535
</program>
3636

3737
<p>
38-
The FileWriter class can be used to write to files. In the same way that the Scanner class isn't needed unless the program will read from a file, the FileWriter class isn't needed unless the program will write to a file.
38+
The <c>FileWriter</c> class can be used to write to files. In the same way that the <c>Scanner</c> class isn't needed unless the program will read from a file, the <c>FileWriter</c> class isn't needed unless the program will write to a file.
3939
</p>
4040

4141
<program interactive="activecode" language="java">
@@ -45,7 +45,7 @@
4545
</program>
4646

4747
<p>
48-
Finally, these last two classes provide error handling and must be used in tandem with the File class when reading from or writing to files. IOException handles file creation and writing errors, while FileNotFoundException handles errors when trying to read files.
48+
Finally, these last two classes provide error handling and must be used in tandem with the <c>File</c> class when reading from or writing to files. <c>IOException</c> handles file creation and writing errors, while <c>FileNotFoundException</c> handles errors when trying to read files.
4949
</p>
5050

5151
<program interactive="activecode" language="java">
@@ -66,7 +66,7 @@
6666
<title>Creating Files</title>
6767

6868
<p>
69-
Before we can write code that creates a file, we must first import the necessary classes mentioned in the previous section (Scanner and FileWriter are not needed for a class that only creates files) and create a class. We will call this class CreateFile.
69+
Before we can write code that creates a file, we must first import the necessary classes mentioned in the previous section (<c>Scanner</c> and <c>FileWriter</c> are not needed for a class that only creates files) and create a class. We will call this class <c>CreateFile</c>.
7070
</p>
7171

7272
<program interactive="activecode" language="java">
@@ -83,7 +83,7 @@
8383
</program>
8484

8585
<p>
86-
Next, within the main function, we will create a File object. It is important to create a meaningful name for the File object. We will call ours <c>myFile</c>.
86+
Next, within the main function, we will create a <c>File</c> object. It is important to create a meaningful name for the <c>File</c> object. We will call ours <c>myFile</c>.
8787
</p>
8888

8989
<program interactive="activecode" language="java">
@@ -94,19 +94,19 @@
9494

9595
<note>
9696
<p>
97-
myFile is the name of the object within the program, while "myfile.txt" is the name of the file itself and will be the file name if the operation that creates the file is successful.
97+
<c>myFile</c> is the name of the object within the program, while <c>myfile.txt</c> is the name of the file itself and will be the file name if the operation that creates the file is successful.
9898
</p>
9999
</note>
100100

101101
<p>
102-
Now that we have created a new File object, we can create a file using the <c>createNewFile()</c> method from the File class. While the previous line of code creates an object within the program for the file, this method actually does the work of creating a file and saving it in the current working directory. This method returns a boolean value. If the method returns <c>true</c>, the file was successfully created. If the method returns <c>false</c>, there is already a file using the chosen file name. We can use this method's possible return values in tandem with an if/else selection to determine if the file was created, or if a file with that file name already exists in the directory.
102+
Now that we have created a new <c>File</c> object, we can create a file using the <c>createNewFile()</c> method from the <c>File</c> class. While the previous line of code creates an object within the program for the file, this method actually does the work of creating a file and saving it in the current working directory. This method returns a boolean value. If the method returns <c>true</c>, the file was successfully created. If the method returns <c>false</c>, there is already a file using the chosen file name. We can use this method's possible return values in tandem with an if/else selection to determine if the file was created, or if a file with that file name already exists in the directory.
103103
</p>
104104

105105
<p>
106106
First, lets look at the equivalent Python code:
107107
</p>
108108

109-
<program language="python">
109+
<program interactive="activecode" language="python">
110110
<code>
111111
import os
112112

@@ -149,7 +149,7 @@
149149
</note>
150150

151151
<p>
152-
The code may seem complete at this point, but if you remember from the previous section, error handling using the IOException is required for program to compile. Let's utilize best practices and add in try/catch blocks to handle exceptions thrown by the IOException class.
152+
The code may seem complete at this point, but if you remember from the previous section, error handling using the <c>IOException</c> is required for program to compile. Let's utilize best practices and add in try/catch blocks to handle exceptions thrown by the <c>IOException</c> class.
153153
</p>
154154

155155
<program interactive="activecode" language="java">
@@ -169,7 +169,7 @@
169169

170170
<note>
171171
<p>
172-
The <c>IOException e</c> part in the parenthesis next to the catch. This creates a variable called <c>e</c> that refers to an IOException object. In other words, <c>e</c> refers to the error created if the try block fails. The line <c>e.printStackTrace();</c> prints the stack trace to the console. This is what the console may output if the program tries to create a file, but is blocked by the Operating System due to insufficient permissions:
172+
The <c>IOException e</c> part in the parenthesis next to the catch. This creates a variable called <c>e</c> that refers to an IOException object. In other words, <c>e</c> refers to the error created if the try block fails. The line <c>e.printStackTrace();</c> prints the stack trace to the console. This is what the console may output if the program tries to create a file, but is blocked by the operating system due to insufficient permissions:
173173
</p>
174174
</note>
175175

@@ -191,7 +191,7 @@
191191
First, the equivalent Python code:
192192
</p>
193193

194-
<program language="python">
194+
<program interactive="activecode" language="python">
195195
<code>
196196
import os
197197

@@ -240,7 +240,7 @@
240240
</program>
241241

242242
<p>
243-
You may be wondering: "What if I don't want to create a file in the current working directory?" Good question! In Windows environments, you can specify the file path using two back slashes for each back slash in the file path. For each pair of back slashes, the first backslash acts as an escape character. So, if you want to save a file to this directory:
243+
You may be wondering: "What if I don't want to create a file in the current working directory?" Good question! In Windows environments, you can specify the file path using two back slashes for each back slash in the file path. For each pair of back slashes, the first back slash acts as an escape character. So, if you want to save a file to this directory:
244244
</p>
245245

246246
<pre>
@@ -272,11 +272,11 @@
272272
<title>Writing to Files</title>
273273

274274
<p>
275-
The <c>createNewFile()</c> method is useful for attempting to create files and reporting if the operation was successful, however, <c>createNewFile()</c> does not write anything to files it creates. In fact, if you use <c>createNewFile()</c> to create a .txt file and then open the file, the file will be blank.
275+
The <c>createNewFile()</c> method is useful for attempting to create files and reporting if the operation was successful, however, <c>createNewFile()</c> does not write anything to files it creates. In fact, if you use <c>createNewFile()</c> to create a <c>.txt</c> file and then open the file, the file will be blank.
276276
</p>
277277

278278
<p>
279-
To write to a file, we will need to create a different class. We will do the same setup as the previous section. First, we will import the classes (File and Scanner are not needed) and create the framework for a class that will write to a file. Let's call this class WriteFile:
279+
To write to a file, we will need to create a different class. We will do the same setup as the previous section. First, we will import the classes (<c>File</c> and <c>Scanner</c> are not needed) and create the framework for a class that will write to a file. Let's call this class <c>WriteFile</c>:
280280
</p>
281281

282282
<program interactive="activecode" language="java">
@@ -293,7 +293,7 @@
293293
</program>
294294

295295
<p>
296-
Next, we will create a FileWriter object. Let's call it myWriter:
296+
Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>:
297297
</p>
298298

299299
<program interactive="activecode" language="java">
@@ -303,7 +303,7 @@
303303
</program>
304304

305305
<p>
306-
In this next step, we will use the <c>write()</c> method from the FileWriter class. This Method will take any data within the parenthesis and write that data to the file selected. The write() method takes most standard data types:
306+
In this next step, we will use the <c>write()</c> method from the FileWriter class. This Method will take any data within the parenthesis and write that data to the file selected. The <c>write()</c> method takes most standard data types:
307307
</p>
308308

309309
<program interactive="activecode" language="java">
@@ -320,10 +320,10 @@
320320
</note>
321321

322322
<p>
323-
Next, we will again add the required try/catch blocks utilizing the IOException class. Just like with creating files, the program will not compile without these crucial additions! We will also add some print statements to inform us of the success of the file write operation. First, a Python example:
323+
Next, we will again add the required try/catch blocks utilizing the <c>IOException</c> class. Just like with creating files, the program will not compile without these crucial additions! We will also add some print statements to inform us of the success of the file write operation. First, a Python example:
324324
</p>
325325

326-
<program language="python">
326+
<program interactive="activecode" language="python">
327327
<code>
328328
try:
329329
with open("myfile.txt", "w") as my_writer:
@@ -358,7 +358,7 @@
358358
And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code:
359359
</p>
360360

361-
<program language="python">
361+
<program interactive="activecode" language="python">
362362
<code>
363363
try:
364364
with open("myfile.txt", "w") as my_writer:
@@ -402,12 +402,12 @@
402402

403403
<note>
404404
<p>
405-
If a file does not already exist (for example, myfile.txt does not exist), the <c>write()</c> method will create the file. Despite this, it is still a good idea to create separate methods or classes for creating and writing to files. Not only is it good practice to ensure methods only accomplish one thing, but the <c>createNewFile()</c> method avoids overwriting files that already exist. Imagine a file with the name myfile.txt already exists and contains important information. Attempting to create a file using the <c>write()</c> method will delete that data forever.
405+
If a file does not already exist (for example, <c>myfile.txt</c> does not exist), the <c>write()</c> method will create the file. Despite this, it is still a good idea to create separate methods or classes for creating and writing to files. Not only is it good practice to ensure methods only accomplish one thing, but the <c>createNewFile()</c> method avoids overwriting files that already exist. Imagine a file with the name <c>myfile.txt</c> already exists and contains important information. Attempting to create a file using the <c>write()</c> method will delete that data forever.
406406
</p>
407407
</note>
408408

409409
<p>
410-
Speaking of overwriting data, what if we want to append text to the end of any text already in myfile.txt? To accomplish this, we can pass a boolean argument along with the file name when creating a new data argument:
410+
Speaking of overwriting data, what if we want to append text to the end of any text already in <c>myfile.txt</c>? To accomplish this, we can pass a boolean argument along with the file name when creating a new data argument:
411411
</p>
412412

413413
<program interactive="activecode" language="java">
@@ -442,7 +442,7 @@
442442
</program>
443443

444444
<p>
445-
and then run the program twice, the contents of myfile.txt would be:
445+
and then run the program twice, the contents of <c>myfile.txt</c> would be:
446446
</p>
447447

448448
<program language="$">
@@ -538,7 +538,7 @@
538538
The next lines consist of a while loop that reads each line of the file passed to the Scanner object and reads them. First, a Python code example. A for loop is used instead in the Python example:
539539
</p>
540540

541-
<program language="python">
541+
<program interactive="activecode" language="python">
542542
<code>
543543
with open("filename.txt", "r") as file_reader:
544544
for line in file_reader:
@@ -589,7 +589,7 @@
589589
Using the second method of storing all file contents to one file, the resulting full code including try/catch blocks (this time using FileNotFoundException instead of IOException) will look something like this. First, the Python code:
590590
</p>
591591

592-
<program language="python">
592+
<program interactive="activecode" language="python">
593593
<code>
594594
try:
595595
with open("myfile.txt", "r") as file_reader:

0 commit comments

Comments
 (0)