1
00:00:05,200 --> 00:00:09,280
In this video, we'll see another 
way to store data in a text file. 

2
00:00:09,280 --> 00:00:14,000
This time, we'll use the write method.
I'll continue in the same Python file, 

3
00:00:14,000 --> 00:00:18,896
and start by commenting out all the code 
from the last video, except for the list. 

4
00:00:20,160 --> 00:00:24,240
This time, I'll call the 
output file flowers_write.txt. 

5
00:00:24,240 --> 00:00:27,760
That will let us see the difference 
between data that we've stored using print, 

6
00:00:27,760 --> 00:00:31,266
and the data in this video, 
that will be stored using write. 

7
00:01:00,800 --> 00:01:05,440
That's very similar to our 
earlier code, on lines 25 to 27. 

8
00:01:05,440 --> 00:01:10,640
Once again, we iterate over the items in 
the list, and store each item in the file. 

9
00:01:10,640 --> 00:01:15,000
This time, we use the write method 
of our file object – plants. 

10
00:01:15,040 --> 00:01:18,976
The code's very similar, but the 
results will be a bit different. 

11
00:01:19,840 --> 00:01:26,160
Run the program, and let's see what we get.
Just as before, the text file gets created. 

12
00:01:26,160 --> 00:01:32,074
Double-click flowers_write.txt, in the 
Project pane, to open it in our editor. 

13
00:01:35,680 --> 00:01:40,400
That's definitely different to our previous file!
Instead of each plant on a new line, 

14
00:01:40,400 --> 00:01:45,280
we've got a single line of text, with each plant 
written immediately after the previous one. 

15
00:01:45,280 --> 00:01:48,960
That's the most obvious difference 
between using print and using write. 

16
00:01:48,960 --> 00:01:53,120
The write method only sends exactly 
what you specify, to the file. 

17
00:01:53,120 --> 00:01:56,400
print performs various conversions 
on the data, and includes things 

18
00:01:56,400 --> 00:02:00,880
like line breaks, and separators.
Notice that I didn't just mention 

19
00:02:00,880 --> 00:02:06,142
"line breaks and separators" there.
I also mentioned "various conversions". 

20
00:02:07,280 --> 00:02:12,641
The print function calls a special method, that 
all objects have, before printing the object. 

21
00:02:12,720 --> 00:02:18,320
The method's called __str__.
You'll learn the reason for that strange name, 

22
00:02:18,320 --> 00:02:23,440
with underscores, in the OOP section.
What the method does, is return a string 

23
00:02:23,440 --> 00:02:28,468
representation of the object. That's what 
allows print to print objects to the screen. 

24
00:02:28,468 --> 00:02:37,191
I'll add some code, to give some examples of this.
Back in IntelliJ, I'll print our data list: 

25
00:02:45,840 --> 00:02:48,276
Run the program

26
00:02:48,276 --> 00:02:52,748
and we can see the string representation
of our list, in the output. 

27
00:02:53,520 --> 00:02:57,520
The string representation encloses the list 
in square brackets – which is what we use to 

28
00:02:57,520 --> 00:03:02,851
define our list. So when we print a list, 
it looks how we'd expect a list to look. 

29
00:03:02,960 --> 00:03:08,080
Each string item in the list is enclosed in 
single quotes, and they're separated by commas. 

30
00:03:08,080 --> 00:03:14,737
Let's check what that __str__ method returns, 
just to be clear about what print is doing for us: 

31
00:03:32,720 --> 00:03:39,186
Run the program
and string_representation is a string. 

32
00:03:39,600 --> 00:03:44,497
So why am I telling you all this?
Remember that text files can only hold text. 

33
00:03:44,640 --> 00:03:48,640
If you try to put numbers into 
a text file, things don't work. 

34
00:03:48,640 --> 00:03:52,497
But you can put the string representation 
of a number into a text file. 

35
00:03:52,560 --> 00:03:56,401
That means we can print numbers to 
a file, but we can't write them. 

36
00:03:56,401 --> 00:04:01,440
I'll finish this video by demonstrating that. 
Comment out the existing code, and we'll try to 

37
00:04:01,440 --> 00:04:05,904
print, and write, numerical values to a text file.

38
00:04:37,155 --> 00:04:45,456
That looks straightforward. We print the numbers
from 0 to 9, to a text file called test_numbers.txt. 

39
00:04:46,800 --> 00:04:53,404
Run the program,
and open test_numbers.txt in IntelliJ. 

40
00:04:56,320 --> 00:05:01,160
No surprises there. The print function obtained 
the string representation of each number, 

41
00:05:01,160 --> 00:05:07,239
and sent it to the file. Because print automatically 
adds a newline, each number is on a new line. 

42
00:05:07,440 --> 00:05:11,068
If we do the same thing with the 
write method, it doesn't work: 

43
00:05:37,520 --> 00:05:40,889
You may get a warning from your IDE, on line 53, 

44
00:05:40,889 --> 00:05:44,640
that a string type was expected.
We're writing to a text file, 

45
00:05:44,640 --> 00:05:50,560
which means we can only put strings into it.
But let's run the program, and see what happens. 

46
00:05:50,560 --> 00:05:55,456
And we get a TypeError: "write() 
argument must be str, not int". 

47
00:05:56,640 --> 00:05:58,880
The print function will print 
the string representation 

48
00:05:58,880 --> 00:06:03,680
of any object that you ask it to print.
In addition, it will include a separator between 

49
00:06:03,680 --> 00:06:09,520
multiple arguments – the default is a space, but 
that can be changed with the sep keyword argument. 

50
00:06:09,520 --> 00:06:12,880
Finally, it will print the value 
of the end keyword argument. 

51
00:06:12,880 --> 00:06:16,273
The default is a newline character. 

52
00:06:17,440 --> 00:06:20,800
The write method will only write 
exactly what you tell it to write. 

53
00:06:20,800 --> 00:06:26,160
No separators or newline characters are included, 
unless you explicitly tell it to write them. 

54
00:06:26,160 --> 00:06:30,824
Also, no conversion is performed. If 
you tell write to write an integer, 

55
00:06:30,824 --> 00:06:35,040
that's what it will try to send to the file.
If the file is opened in text mode 

56
00:06:35,040 --> 00:06:39,773
(the default), you'll get an error if 
you try to write numerical values to it. 

57
00:06:41,680 --> 00:06:46,480
So which should I use, write or print?
The answer to that is, use whichever is more 

58
00:06:46,480 --> 00:06:50,480
convenient for the task you're trying to perform.
If you want complete control over 

59
00:06:50,480 --> 00:06:54,800
what gets written, use write.
If it's more convenient to let print include 

60
00:06:54,800 --> 00:07:00,560
separators and newline, and convert objects to 
their string representations, then use print. 

61
00:07:00,560 --> 00:07:06,486
Those are guidelines, there are no rules. 
The same is true of human languages. 

62
00:07:08,400 --> 00:07:12,080
For example:
"Do you want to come to the movies with me?" 

63
00:07:12,080 --> 00:07:15,760
and
"I'm going to the movies, do you want to come?" 

64
00:07:15,760 --> 00:07:20,240
both convey pretty much the same thing. 
There are subtle differences, but generally 

65
00:07:20,240 --> 00:07:24,160
you could phrase the question either way.
The first might be more appropriate if you 

66
00:07:24,160 --> 00:07:30,320
were asking for a date. The second makes your 
intention – to see a movie – more explicit. 

67
00:07:30,320 --> 00:07:34,690
You're going to the movies, regardless 
of the answer to the second question. 

68
00:07:36,000 --> 00:07:39,200
Ok, back in IntelliJ, I'll 
change that code to write 

69
00:07:39,200 --> 00:07:42,080
the string representation of our integer values. 

70
00:07:42,080 --> 00:07:46,383
To get the string representation of 
an object, we use the str function: 

71
00:07:55,520 --> 00:08:01,200
Run the program, and let's see what we get.
Open test_numbers.txt, 

72
00:08:01,200 --> 00:08:06,333
and we've got each number printed.
Note that there are no spaces or newline characters. 

73
00:08:06,333 --> 00:08:11,200
The write method writes exactly what 
we tell it to write – nothing less, nothing more. 

74
00:08:11,200 --> 00:08:15,920
If you want each number on a new line, you 
have to explicitly write the newline character. 

75
00:08:15,920 --> 00:08:20,269
One way to do that, is to concatenate the 
newline onto the string we're writing: 

76
00:08:24,000 --> 00:08:29,739
Run the program again.
Then check test_numbers.txt. 

77
00:08:29,840 --> 00:08:34,559
Now, each number is on a new line.
Ok, we've seen two different ways to 

78
00:08:34,559 --> 00:08:38,640
write to a text file, and discussed 
the main differences between them. 

79
00:08:38,640 --> 00:08:42,159
One thing you might be wondering is, what 
happened to the numbers that we printed, 

80
00:08:42,159 --> 00:08:48,720
on lines 47 to 49 of our code?
I'll switch back to save_flowers.py in IntelliJ. 

81
00:08:48,720 --> 00:08:53,040
On lines 47 to 49, we printed 
the numbers to the file. 

82
00:08:53,040 --> 00:08:57,360
But when we ran the program, after 
adding the code on lines 51 to 53, 

83
00:08:57,360 --> 00:09:04,240
those numbers had disappeared.
We'll find out what happened, in the next video.

