Locked lesson.
About this lesson
There are several "modes" for opening files. Learn them all here.
Exercise files
Download this lesson’s related exercise files.
Open File Modes - Solution.docx59.2 KB Open File Modes.docx
57.7 KB
Quick reference
Open File Modes
There are several ways to open files, depending on what you want to do with them.
When to use
Use any of these open file modes depending on what type of thing you wish to do with the file.
Instructions
Here's a list of open file modes:
r Read only, pointer at beginning of file
r+ Read-Write, pointer at beginning of file
w Write-Only, erases old file contents
w+ Read-Write, erases old file contents
a Write-Only, pointer at end of file
a+ Read-Write, pointer at end of file
To use a specific mode, just pass it in as a parameter in the File.open() method:
my_file = File.new("file.txt", "a+")
Hints & tips
- r, r+, w, w+, a, a+
- Stay away from w and w+ unless you want to erase what's already on a file!
- 00:04 In the last video we talked about sticking the stuff from our file into an array.
- 00:08 In this video I want to talk about open modes.
- 00:11 So here we have this file class in this open method.
- 00:14 And when you use the open method you sort of have to designate the mode
- 00:18 of the method that you want to use.
- 00:20 Now we didn't do that here, we just use the default.
- 00:23 And the default is r, read only.
- 00:26 So when we open this file,
- 00:27 all we could do with it using this line of code is to read it.
- 00:31 We couldn't make any changes, we couldn't make any alterations,
- 00:34 we couldn't do anything to it except for read it.
- 00:37 And a lot of times that's all you need.
- 00:38 You just need to open a file and read it and then pull the stuff out of it and
- 00:41 do stuff to it.
- 00:42 But if you need to do something to the file, if you want to for instance,
- 00:47 add Tina to this file, you couldn't do that using this mode because
- 00:51 this default mode is just read only.
- 00:53 So what are the different modes?
- 00:55 Well, there's a bunch of different modes.
- 00:57 The main ones are r, r+, w, w +, a and a +.
- 01:02 So I'm going to go through all of those r stands for read,
- 01:05 w stands for write,
- 01:06 and a stands for append.
- 01:07 First off, I mentioned this is the default if you don't do anything, so
- 01:12 r read only is the default.
- 01:14 But to change the mode all we do is add a comma to our parameters here that we're
- 01:18 passing into our method.
- 01:20 And do another quotation mark and say, r, so this is read-only.
- 01:25 Now if we save this and run it, we get Bob because we've asked for
- 01:29 the second item in our array.
- 01:32 If we save it like this and run it again, puts my_array there we go.
- 01:38 There we go, we get the full list.
- 01:40 So like I said r is the default, r is read only mode but
- 01:43 the file pointer is placed at the beginning of the file.
- 01:47 And think of the file pointer as this cursor, right?
- 01:51 So if we open this file, the cursor starts right here at the top.
- 01:54 And then our program says, hey, get all the stuff from the files.
- 01:58 Our cursor kind of drags down and highlights everything.
- 02:01 sort of think of it like this.
- 02:02 So with r, the cursor, the file pointer, starts at the top.
- 02:06 So next we move to r+, this stands for read and write, it's a read/write mode.
- 02:12 So it's read plus, read and write.
- 02:15 And with r+, again, the file pointer starts at the beginning of the file,
- 02:20 right?
- 02:20 So next we have w, and this is write only mode.
- 02:24 And again, the file pointer starts at the beginning of the file.
- 02:26 So if you only want to write to the file, use w.
- 02:31 If you want to read and write use r+, right?
- 02:35 So next we have w+ and that's read/write mode.
- 02:39 But the thing about this one is it overwrites the entire file if it already
- 02:44 exists.
- 02:45 So our stuff text if it has stuff in it, it's going to erase everything on there
- 02:49 and it's going to write in whatever you do.
- 02:51 Now if the file doesn't exist, this w+ will create that file.
- 02:55 So that's w.
- 02:56 Next we have a and this is write only mode.
- 02:59 The difference between this and a is the file pointer.
- 03:02 So with w, which is write only mode,
- 03:04 the file pointer starts at the top of the file.
- 03:07 So if you're going to write to this, if we wanted to add Tina using w,
- 03:11 it would go right here, because the cursor starts at the top.
- 03:15 With the a the file pointer is at the end of the file.
- 03:18 So if we wanted to add Tina using a it would look like that.
- 03:22 So, that's a distinction.
- 03:23 And finally we have a+ and this is read in right mode but
- 03:27 the file pointer is at the end of the file if the file exist.
- 03:30 a+ is read and write mode again, but the final pointer is at the end of the file.
- 03:35 Of course, if the file doesn't exist, this will also create the file.
- 03:38 So I've thrown a bunch of these at you, and it's kind of quick.
- 03:41 I'll put a list of these in the resource file, so you can just look at them and
- 03:45 say, okay this is the one I want to use until you know.
- 03:47 So in the next video, we'll talk about writing to files.
Lesson notes are only available for subscribers.