1
00:00:00,000 --> 00:00:03,990
Python has some very simple built- in functions for dealing with files.

2
00:00:04,000 --> 00:00:06,990
Let's make a working copy of files.py.

3
00:00:07,000 --> 00:00:13,990
I'll call it files-working.py and we will open that working file and we see here

4
00:00:14,000 --> 00:00:18,990
a simple loop that reads lines from a file and prints them out.

5
00:00:19,000 --> 00:00:20,990
So run that and see what it does.

6
00:00:21,000 --> 00:00:24,990
It prints out the lines of this file. This is the file here, lines.txt, and we

7
00:00:25,000 --> 00:00:28,990
are just printing out all five lines with that file in this little loop.

8
00:00:29,000 --> 00:00:31,990
What we are going to look at in this movie is this open function.

9
00:00:32,000 --> 00:00:35,990
It's very simple and it has a few options.

10
00:00:36,000 --> 00:00:40,990
By default, all it does is it opens the files in the read mode and you give it a

11
00:00:41,000 --> 00:00:45,990
file name here and it returns the file handle object, which can then be used to

12
00:00:46,000 --> 00:00:47,990
operate on the file.

13
00:00:48,000 --> 00:00:52,990
If you don't specify it, read mode is the default and that looks like this.

14
00:00:53,000 --> 00:00:56,990
There is the second argument, which is the mode, and that defaults to the

15
00:00:57,000 --> 00:00:58,990
r character for read.

16
00:00:59,000 --> 00:01:05,990
Options there are r for read, w for write, or a for append.

17
00:01:06,000 --> 00:01:06,990
Append is a special write mode.

18
00:01:07,000 --> 00:01:10,990
It sets the current position of the file to the end of the file so that

19
00:01:11,000 --> 00:01:14,990
everything that you write will get appended to the file.

20
00:01:15,000 --> 00:01:18,990
In read mode, you can optionally give it a plus sign, which will open it for both

21
00:01:19,000 --> 00:01:22,990
read and write in the same file handle object.

22
00:01:23,000 --> 00:01:28,990
You can also give it a t for text file mode or a b for binary mode, and we will

23
00:01:29,000 --> 00:01:32,990
look at these options a little later in our movie on the subject.

24
00:01:33,000 --> 00:01:38,990
The open function returns this file handle object, which we see used here.

25
00:01:39,000 --> 00:01:43,990
It is an iterable object and in iteration mode it simply gives you one line

26
00:01:44,000 --> 00:01:44,990
of the file at a time.

27
00:01:45,000 --> 00:01:50,990
In fact, there is a method called read lines, which does exactly the same thing.

28
00:01:51,000 --> 00:01:54,990
You save that and run it, and there is our lines of text.

29
00:01:55,000 --> 00:01:58,990
So that is the open function in all of its simplicity.

30
00:01:59,000 --> 00:02:01,990
In the rest of this chapter we will look at how to read and write files and how

31
00:02:02,000 --> 00:02:12,000
to set its file mode.

