Locked lesson.
About this lesson
How to rename and delete a file with Python.
Exercise files
Download this lesson’s related exercise files.
Renaming and Deleting Files.docx59.3 KB Renaming and Deleting Files - Solution.docx
59.3 KB
Quick reference
Renaming and Deleting Files
In this video we'll learn how to rename and delete files.
When to use
Use this whenever you want to rename or delete a file.
Instructions
To rename a file, use the os module (that we've imported previously.)
os.rename("names.txt", "new_names.txt")
That will rename the file from names.txt to it's new name of new_names.txt
To delete a file, we just remove it:
os.remove("names.txt")
Hints & tips
- Use the os module to rename files
- os.rename("names.txt", "new_names.txt") # change the name of the file to new_names.txt
- os.remove("names.txt") # delete (remove) a file
- 00:04 In this video, I want to talk about renaming files and deleting files.
- 00:07 So we've got a file, it's named names.txt and
- 00:11 let's say we need to rename that file for some reason.
- 00:14 It doesn't really matter why.
- 00:15 So how do we do that?
- 00:16 Well, the main way to do it is we're going to use this OS module and
- 00:20 OS stands for operating system.
- 00:22 And if you think about it,
- 00:23 renaming a file that's sort of an operating system type thing, right?
- 00:27 So, it kind of makes sense that you would use the OS module to do that.
- 00:31 So it's very simple.
- 00:32 We just call the module OS and then the function rename,
- 00:36 and then pass it in some parameters.
- 00:38 Like all functions,
- 00:39 it has these parentheses where you pass in different parameters.
- 00:42 And there's two parameters we're going to pass in.
- 00:44 We're going to pass in the, say the current_file_name, and then a comma,
- 00:50 and then whatever new_file_name we want to rename it to, right.
- 00:56 So, let's say we wanted to rename our file first names instead of names,
- 01:01 so we would go os.rename.
- 01:03 So let's go names.txt, that's the name of our current file.
- 01:09 And then let's rename it to like I said, first_names.txt and
- 01:13 I'm going to go ahead and comment this line out because we don't want this to run.
- 01:17 So, here we have current name and then the new name.
- 01:21 Pretty simple, we save this and run it, nothing happens, but boom,
- 01:24 right here we see it's changed to first_names.txt.
- 01:27 And if we wanted to play around a little bit we could go,
- 01:31 print("Your File Name Has Been Changed!").
- 01:35 Okay, so let's say we want to change it back from first_names back to names, so
- 01:40 just make a quick little change.
- 01:42 Save this, run it.
- 01:44 Now we get a little prompt, your file name has been changed and sure enough,
- 01:47 we look up here it's been changed.
- 01:49 The contents still remain the same, everything is fantastic.
- 01:52 So super, super easy to rename a file with Python, you just use the OS module.
- 01:57 And of course, you can go look up the OS module if you want to learn more
- 02:01 information about what all it does.
- 02:03 We looked at that Python three documentation page that has all those
- 02:06 built in modules, so you could check that out.
- 02:08 To delete a file, super easy as well.
- 02:10 We just used the remove function and it's not deleting, we're removing it.
- 02:15 It's what Python calls it.
- 02:16 So let's say your file has been removed, change that around.
- 02:20 So to do that, comment that out, we just go os.remove and it's a function,
- 02:27 and we pass in the parameter, whatever name we want to remove.
- 02:30 So let's get rid of names.txt, right?
- 02:33 So if you save this, run it, your file name has been removed.
- 02:37 And if we look up here in our directory, sure enough,
- 02:39 it has absolutely disappeared.
- 02:41 So, I don't know, our name is gone.
- 02:43 We learn how to write to a file in the last video, so
- 02:48 if we want to bring it back, we can go my_file = open and
- 02:53 let's call it names.txt.
- 02:56 And we want to open it in write only because that will add the thing if it
- 03:01 hasn't already been added.
- 03:03 And if we save this and run it, boom, names.txt reappears, of course,
- 03:07 it doesn't have anything in it.
- 03:08 We could write stuff in it, like we learned how to do in the last video,
- 03:11 if we wanted to, super easy.
- 03:13 Just, my_file.write, and let's go,
- 03:18 John, Tim, or was it Mary and Bob, I believe.
- 03:25 Save this, run it, open this up again, reload.
- 03:28 There we go John, Tim, Mary and Bob.
- 03:30 So, that's how you rename files, that's how you delete files,
- 03:33 that's how you create new files out of thin air
- 03:35 after you've already deleted them, and that pretty much does it for file stuff.
- 03:40 And in fact that does it for their intermediate section.
- 03:42 In the next section, we're going to go on into advanced Python stuff, specifically,
- 03:46 we're going to look at classes.
- 03:47 In the next video, we're going to look at an overview of classes and
- 03:52 object-oriented programming.
Lesson notes are only available for subscribers.