1
00:00:05,120 --> 00:00:09,656
You've seen the basics of reading from 
a text file, in the last few videos. 

2
00:00:09,760 --> 00:00:12,320
The steps are:
Open the file 

3
00:00:12,320 --> 00:00:15,516
Read the data, and
Close the file when you're done.

4
00:00:15,516 --> 00:00:19,840
After reading the data, you'd either process 
it in some way, or store it somewhere, 

5
00:00:19,840 --> 00:00:22,680
but those are the basic steps you'd perform. 

6
00:00:24,960 --> 00:00:28,379
The basic steps when writing 
data to a file are the same: 

7
00:00:28,379 --> 00:00:30,720
Open the file
Write the data

8
00:00:30,720 --> 00:00:33,920
and Close the file when you've finished writing. 
You'd have some way to 

9
00:00:33,920 --> 00:00:39,073
generate the data that you want to write, 
of course, but those are the basic steps. 

10
00:00:41,280 --> 00:00:44,720
It's also possible to append 
data to an existing file. 

11
00:00:44,720 --> 00:00:49,120
We're going to start by creating a new file each 
time, but it's also possible to open a file and 

12
00:00:49,120 --> 00:00:53,270
write more data to the end of it.
We'll see how to do that later. 

13
00:00:54,720 --> 00:01:00,449
For now, back in IntelliJ, create a 
new Python file called save_flowers.py. 

14
00:01:00,720 --> 00:01:06,000
Back in the Lists and Tuples section, we worked 
with a list of plants in one of the exercises. 

15
00:01:06,080 --> 00:01:11,612
We'll use that same list for this example.
Download plants.txt from the resources for this video, 

16
00:01:11,612 --> 00:01:15,124
and paste the list 
into your save_flowers file: 

17
00:01:15,200 --> 00:01:18,503
Ok, that's the data that 
we're going to save in a file. 

18
00:01:18,560 --> 00:01:23,299
We'll call the file flowers_print.txt, 
and I'll store the name in a variable: 

19
00:01:30,960 --> 00:01:36,080
We want to write to this file. It's almost 
the same as reading a file, but we specify 

20
00:01:36,080 --> 00:01:39,936
"w" for the mode. That's "w" for write.

21
00:01:54,879 --> 00:01:59,613
After opening the file for writing, we
iterate over the items in our data list.

22
00:01:59,613 --> 00:02:03,040
Inside the loop, we'll send
each item to the file.

23
00:02:03,040 --> 00:02:06,481
That's easy, but there are 
two different ways to do it. 

24
00:02:06,560 --> 00:02:12,259
One way is just to print the data. I'll do that, 
then we'll examine what gets put into the file: 

25
00:02:20,240 --> 00:02:24,480
If you remember, when we looked at the signature 
for the print function, there were some keyword 

26
00:02:24,480 --> 00:02:30,157
arguments that we didn't cover, at the time.
The file keyword argument was one of those. 

27
00:02:30,157 --> 00:02:35,251
It tells print where it should print the data to.
The default is to print to the screen. 

28
00:02:35,251 --> 00:02:38,720
That's most often what you want to do, 
which is why it's the default. 

29
00:02:38,720 --> 00:02:43,760
But you can print to any output stream that 
can accept text – including to an open file. 

30
00:02:43,760 --> 00:02:47,761
That's what we do here – we tell 
print to send the text to our file. 

31
00:02:47,920 --> 00:02:51,200
That doesn't look too complicated. 
Anything that we can print to the 

32
00:02:51,200 --> 00:02:57,408
console can also be sent to a file, instead.
Let's run the program, and check what we get. 

33
00:02:58,800 --> 00:03:03,489
In the Project pane, we can see 
flowers_print.txt has been created. 

34
00:03:03,600 --> 00:03:06,386
Double-click it, to open it in our IDE. 

35
00:03:08,560 --> 00:03:13,120
The contents of this file look exactly the same 
as the output we'd have got by printing the list. 

36
00:03:13,120 --> 00:03:17,360
The file contains 19 strings, each 
ending with a newline character. 

37
00:03:17,360 --> 00:03:22,663
The print function automatically adds a newline, 
when it prints, so that's what we got in the file. 

38
00:03:22,720 --> 00:03:27,583
This is one difference between this way of writing 
to a file, and the way that we'll look at next. 

39
00:03:27,680 --> 00:03:31,120
Before we see the other way, 
let's read the data back. 

40
00:03:31,120 --> 00:03:35,550
Writing data to a file isn't very useful, 
if we can't read it back in again. 

41
00:04:08,320 --> 00:04:14,080
After opening the file for reading, we iterate 
over it, and add each string to the new_list list. 

42
00:04:14,080 --> 00:04:18,914
We also strip off the newline characters 
from each string that we read, on line 32. 

43
00:04:18,959 --> 00:04:25,201
We've seen code like that, in the earlier videos.
Run the program, and check the output. 

44
00:04:25,280 --> 00:04:29,520
We've got our original list back.
Printing data to a file is quite easy, 

45
00:04:29,520 --> 00:04:33,761
using print. But this isn't 
the only way, as I mentioned. 

46
00:04:33,920 --> 00:04:35,966
I'll see you in the next video.

