1 00:00:00,735 --> 00:00:04,290 I've found in my Python travels that I usually care more about deleting files and 2 00:00:04,290 --> 00:00:06,190 directories, than I do moving them around. 3 00:00:06,190 --> 00:00:09,402 In fact, I might have done more deletion than creation even since, quite often, 4 00:00:09,402 --> 00:00:11,220 files get added through version controller or 5 00:00:11,220 --> 00:00:14,380 user uploads, and then I need to clean them up with Python. 6 00:00:14,380 --> 00:00:17,150 So let's look at how to delete files and directories with Python. 7 00:00:17,150 --> 00:00:20,120 But be warned, Python deletes items immediately. 8 00:00:20,120 --> 00:00:23,630 The files won't be moved to the users trash or any equivalent location. 9 00:00:23,630 --> 00:00:26,320 We'll look at a handy third-party library, though, named Send2Trash, 10 00:00:26,320 --> 00:00:28,060 that will help us avoid this behavior. 11 00:00:29,660 --> 00:00:33,060 We're gonna look at three different functions for removing things and, 12 00:00:33,060 --> 00:00:36,190 unfortunately, you kinda have to know all three of them. 13 00:00:36,190 --> 00:00:37,475 They each do different things and 14 00:00:37,475 --> 00:00:40,010 you'll often need to use at least two of them together. 15 00:00:41,060 --> 00:00:43,700 Let's start with the function that lets us delete files. 16 00:00:43,700 --> 00:00:46,800 So I've created a file here in the JavaScript directory 17 00:00:46,800 --> 00:00:48,450 that's called treehouse.js. 18 00:00:48,450 --> 00:00:51,810 There's nothing in there, it's just a file I wanted to have on hand to delete. 19 00:00:51,810 --> 00:00:55,750 So now I can do os.remove, and 20 00:00:55,750 --> 00:01:01,100 I can say bootstrap/js/treehouse.js. 21 00:01:01,100 --> 00:01:04,140 And if I look now, the file's gone. 22 00:01:04,140 --> 00:01:07,520 The remove function lets us, well, remove a file. 23 00:01:07,520 --> 00:01:08,860 Now if you're using Windows and 24 00:01:08,860 --> 00:01:12,750 you try to remove a file that's currently in use, Python is going to throw an error. 25 00:01:12,750 --> 00:01:16,830 On POSIX systems, it'll remove the directory entry so it wouldn't show up if 26 00:01:16,830 --> 00:01:20,510 you listed the directory, but it doesn't clear away the associated memory. 27 00:01:20,510 --> 00:01:23,710 So the memory is still in use but the file doesn't show up in the directory. 28 00:01:23,710 --> 00:01:27,100 That can only be used to remove files. 29 00:01:27,100 --> 00:01:28,910 So what are directories? 30 00:01:28,910 --> 00:01:33,590 Well, Python requires that directories be empty before you try and delete them. 31 00:01:33,590 --> 00:01:38,029 So you'll probably find yourself using os.remove first and 32 00:01:38,029 --> 00:01:41,962 then you can bust out os.rmdir to remove a directory. 33 00:01:41,962 --> 00:01:47,108 So, for instance, I know that the bootstrap/img directory is empty. 34 00:01:47,108 --> 00:01:49,793 Let's double check that. 35 00:01:49,793 --> 00:01:51,160 And it is because it doesn't even exist. 36 00:01:51,160 --> 00:01:53,180 Let's create a new folder here and we'll call it img. 37 00:01:53,180 --> 00:01:55,850 So there's nothing in there. 38 00:01:55,850 --> 00:01:58,180 And now if we remove it, it's gone. 39 00:01:58,180 --> 00:02:01,790 Great, the directory is empty, Python is happy to delete it. 40 00:02:01,790 --> 00:02:07,590 But if I try to do os.rmdir bootstrap/js, which we just saw 41 00:02:07,590 --> 00:02:12,420 has several files in it, I get an error saying that it doesn't want to do that. 42 00:02:13,930 --> 00:02:15,440 I also misspelled bootstrap. 43 00:02:16,750 --> 00:02:20,820 There we go, there's our OSError of the directory isn't empty. 44 00:02:20,820 --> 00:02:24,630 That says the js directory isn't empty, Python complains like I just showed you. 45 00:02:24,630 --> 00:02:28,117 We could use os.walk or os.scandir to move through the directory and 46 00:02:28,117 --> 00:02:31,083 delete everything before we remove the directory itself. 47 00:02:31,083 --> 00:02:36,553 We could do, say, for thing in os.scandir, 48 00:02:36,553 --> 00:02:43,581 bootstrap/js/, if thing.is file, so if it's a file, 49 00:02:45,825 --> 00:02:50,165 Then os.remove, thing.path. 50 00:02:50,165 --> 00:02:51,900 Thing.path gives us the path to the file. 51 00:02:53,080 --> 00:02:57,937 And now we can do os.rmdir, bootstrap/js, and 52 00:02:57,937 --> 00:03:01,960 the directory happily is deleted. 53 00:03:01,960 --> 00:03:03,700 So, thanks Python. 54 00:03:03,700 --> 00:03:08,590 Now if I need to delete a tree of empty directories I can use os.rmdirs. 55 00:03:08,590 --> 00:03:13,282 Let go ahead and make one first, let's do os.makedirs, 56 00:03:13,282 --> 00:03:17,307 let's do bootstrap/js/packages/stuff, so 57 00:03:17,307 --> 00:03:21,060 if you had a whole bunch of directories there. 58 00:03:21,060 --> 00:03:26,057 And now we can delete that, we can do os.removedirs, 59 00:03:26,057 --> 00:03:31,070 and we can give it bootstrap/js/packages/stuff. 60 00:03:31,070 --> 00:03:33,170 But that's kind of weird, right? 61 00:03:33,170 --> 00:03:36,120 I said to remove stuff, and 62 00:03:36,120 --> 00:03:41,030 it removed stuff, it removed packages, it removed js, but it didn't 63 00:03:41,030 --> 00:03:44,640 remove bootstrap because I'm currently in the bootstrap directory right here. 64 00:03:45,990 --> 00:03:48,500 This function tries to remove every directory that you specified. 65 00:03:48,500 --> 00:03:51,770 And it stops as soon as it encounters an error though. 66 00:03:51,770 --> 00:03:55,470 And usually that error is because the parent directory isn't empty. 67 00:03:55,470 --> 00:03:57,600 In our case it's because bootstrap wasn't empty. 68 00:03:57,600 --> 00:03:59,340 So Python stops and bootstrap sticks around. 69 00:03:59,340 --> 00:04:01,980 All right, so we can delete files and directories. 70 00:04:01,980 --> 00:04:04,810 But, like I said at the beginning of the video, they're gone, gone, 71 00:04:04,810 --> 00:04:06,980 they're deleted immediately. 72 00:04:06,980 --> 00:04:10,920 We can prevent that by using the Send2Trash package. 73 00:04:10,920 --> 00:04:16,036 I'm gonna hop out of Python here and I'm gonna do pip3 install send2trash. 74 00:04:19,290 --> 00:04:21,160 And then I can use it in Python. 75 00:04:22,710 --> 00:04:26,894 So I'll clear my screen, and I'm gonna say, 76 00:04:26,894 --> 00:04:30,658 from send2trash import send two trash. 77 00:04:30,658 --> 00:04:34,560 And then I'll do, send2trash, tree.py. 78 00:04:34,560 --> 00:04:41,468 And if I look in my trash can, I have tree.py right there in the trash can. 79 00:04:42,896 --> 00:04:45,410 I didn't want to delete it, so it's in my trash, that's great. 80 00:04:45,410 --> 00:04:47,527 I can bring it right back if I need to. 81 00:04:50,834 --> 00:04:55,250 So, let's go up a directory here and I want tree.py to be right there. 82 00:04:56,830 --> 00:04:59,500 Often, when you find yourself needing temporary files or directories, 83 00:04:59,500 --> 00:05:01,790 you resort to a lot of things we've just been doing. 84 00:05:01,790 --> 00:05:03,970 You'll create a directory or a file, fill it with information and 85 00:05:03,970 --> 00:05:06,780 then move it to its final location or delete it. 86 00:05:06,780 --> 00:05:09,320 Well, you know Python, if there's something that's commonly done, 87 00:05:09,320 --> 00:05:11,790 that pattern is probably built into the language. 88 00:05:11,790 --> 00:05:14,630 Temporary files and directories are no exception. 89 00:05:14,630 --> 00:05:17,770 So after this temporary break I'll be back to show you how to create transient 90 00:05:17,770 --> 00:05:19,180 files and directories in Python.