1
00:00:00,000 --> 00:00:03,990
Reading and writing text files in Python is really very simple.

2
00:00:04,000 --> 00:00:09,990
Let's make a working copy of files.py. files-working.py.

3
00:00:10,000 --> 00:00:14,990
Open that working copy and we see that we have the simple loop here that reads

4
00:00:15,000 --> 00:00:16,990
lines of a file and prints them to the screen.

5
00:00:17,000 --> 00:00:21,990
So if we run that, we see there is our lines of file, print it to the screen, and

6
00:00:22,000 --> 00:00:23,990
here is our input file.

7
00:00:24,000 --> 00:00:25,990
So, we can see that that's exactly the same.

8
00:00:26,000 --> 00:00:29,990
Now, if we wanted to, instead of bringing that to the screen, if we wanted to

9
00:00:30,000 --> 00:00:31,990
write it to a file, we could do that.

10
00:00:32,000 --> 00:00:38,990
Simply change this to infile, so we can have a separate one for outfile, outfile

11
00:00:39,000 --> 00:00:41,990
equals open and we'll call this new.txt.

12
00:00:42,000 --> 00:00:44,990
We will open that for write.

13
00:00:45,000 --> 00:00:46,990
And we will open this one here for read.

14
00:00:47,000 --> 00:00:49,990
It defaults to read but if I am going to specify write, then I am going to want

15
00:00:50,000 --> 00:00:52,990
to also specify read.

16
00:00:53,000 --> 00:00:59,990
This is called infile, and all we have to do here is to say file equals outfile,

17
00:01:00,000 --> 00:01:04,990
And now that print is going to print the outfile instead of to the screen.

18
00:01:05,000 --> 00:01:08,990
And as we don't have anything coming to the screen, I am just going to print done here.

19
00:01:09,000 --> 00:01:11,990
So we know that we have gotten to this point.

20
00:01:12,000 --> 00:01:13,990
We get some feedback on the screen.

21
00:01:14,000 --> 00:01:17,990
I will save that and I will run it and it says done.

22
00:01:18,000 --> 00:01:22,990
If I refresh the file system here, which Eclipse does not do by itself, you can

23
00:01:23,000 --> 00:01:26,990
see there is a new.txt and it's got our five lines of text in it.

24
00:01:27,000 --> 00:01:31,990
So that is just a very simple way to write to a text file.

25
00:01:32,000 --> 00:01:35,990
Obviously, it doesn't have to be something you are reading from another text file.

26
00:01:36,000 --> 00:01:39,990
You can generate your own whatever and write it out to a text file very easily,

27
00:01:40,000 --> 00:01:43,990
using print and the File parameter for print.

28
00:01:44,000 --> 00:01:46,990
Let's say that what we are doing is not necessarily line oriented.

29
00:01:47,000 --> 00:01:50,990
Let's say that maybe we have a very large file with thousands of lines of text,

30
00:01:51,000 --> 00:01:52,990
And we don't really need to read them and write them line by line.

31
00:01:53,000 --> 00:01:55,990
We want to do it in some bigger chunks.

32
00:01:56,000 --> 00:02:00,990
So I have a file here called bigfile.txt and you will notice it is a really big file of text.

33
00:02:01,000 --> 00:02:05,990
It has 10,000 lines of text in it.

34
00:02:06,000 --> 00:02:11,990
If we look at the properties, we can see that it's 320,000 bytes. It's 300k.

35
00:02:12,000 --> 00:02:20,990
So, let's go ahead and open that one instead, and call it bigfile.txt and we

36
00:02:21,000 --> 00:02:23,990
are going to read this in what's called buffered mode, instead of doing it line by line.

37
00:02:24,000 --> 00:02:30,990
We will set a buffer size and I will say that's going to be 50,000 bytes.

38
00:02:31,000 --> 00:02:35,990
It's 300,000 bytes, so it will give us a few chunks and then we are going to

39
00:02:36,000 --> 00:02:45,990
start by filling our buffer, infile.read and buffersize and then instead of this

40
00:02:46,000 --> 00:02:50,990
here, I am going to use a While loop, because the Read method on the file handle

41
00:02:51,000 --> 00:02:52,990
object is not an iterable.

42
00:02:53,000 --> 00:02:54,990
So we can't use a For loop, so we are going to use a While loop.

43
00:02:55,000 --> 00:02:58,990
We are going to say while the length of buffer.

44
00:02:59,000 --> 00:03:05,990
And so as long as our buffer is not empty then we will outfile.write buffer.

45
00:03:06,000 --> 00:03:07,990
That's really all there is to it.

46
00:03:08,000 --> 00:03:11,990
We read it again in the loop, so that we are not just writing the same buffer forever.

47
00:03:12,000 --> 00:03:19,990
So I am going to write buffer = infile.read buffersize and after we do the

48
00:03:20,000 --> 00:03:22,990
write, let's just print a dot to the screen, so we have some feedback that we

49
00:03:23,000 --> 00:03:28,990
are doing something, dot, end, equals nothing.

50
00:03:29,000 --> 00:03:32,990
So this will just print dots on the screen right next to each other, and we are all done.

51
00:03:33,000 --> 00:03:36,990
Before the Done, we are going to want to print a blank line.

52
00:03:37,000 --> 00:03:42,990
So we will go ahead and save that and run it and we see we have got a handful of dots there.

53
00:03:43,000 --> 00:03:48,990
It means that we have written something and we refresh our file system and take

54
00:03:49,000 --> 00:03:53,990
a look at the properties of new.txt and it's exactly 320,000 bytes.

55
00:03:54,000 --> 00:03:57,990
We will open that up and there is all of our lines of text in there.

56
00:03:58,000 --> 00:04:01,990
So what we have done here is we simply read it in a few big chunks and

57
00:04:02,000 --> 00:04:05,990
obviously 50,000 bytes is very small by today's standard.

58
00:04:06,000 --> 00:04:08,990
Your buffers can be bigger than that or if you are on a mobile device or

59
00:04:09,000 --> 00:04:11,990
something small, maybe you do want them that small.

60
00:04:12,000 --> 00:04:16,990
We just did that this way for illustration, but this shows you how you can read

61
00:04:17,000 --> 00:04:18,990
and write in buffered I/O mode.

62
00:04:19,000 --> 00:04:24,990
So it allows you to deal with bigger chunks of files than just line by line.

63
00:04:25,000 --> 00:04:28,990
So that's how simple it is to read and write text files in Python and we will

64
00:04:29,000 --> 00:04:39,000
discuss reading and writing binary files in a separate lesson.

