1

00:00:00,240  -->  00:00:05,920
Welcome to lesson 33 which roughly covers pages 200 to 200 to of the army the boring stuff with Python

2

00:00:05,940  -->  00:00:06,870
textbook.

3

00:00:06,890  -->  00:00:11,790
So if you want to delete files and folders there are three functions you can use one of them is in the

4

00:00:11,790  -->  00:00:19,440
os module it's OS dot unlink which is kind of an old name for weed and this one you would just pass

5

00:00:19,440  -->  00:00:24,270
it some file name and this will permanently delete this file.

6

00:00:24,270  -->  00:00:27,150
Now I'm passing it a relative file path hit right here.

7

00:00:27,330  -->  00:00:30,320
This is a file that doesn't begin with the root folder.

8

00:00:30,370  -->  00:00:40,110
If you want to know which begun he XTi Exactly it's the leading call the get C-w function to get the

9

00:00:40,110  -->  00:00:47,400
current working directory so you can figure out OK if I delete if I unlink bacon that text he will actually

10

00:00:47,400  -->  00:00:51,050
delete the bacon that he XTi that's inside this folder.

11

00:00:51,210  -->  00:00:54,210
So unlink will delete a single file.

12

00:00:54,210  -->  00:00:58,170
You can also delete a folder with the Ostara remove directory.

13

00:00:58,170  -->  00:01:05,610
That's our andere function and they can remove some directory as some file or some folder that you pass

14

00:01:05,610  -->  00:01:05,820
it.

15

00:01:05,820  -->  00:01:12,240
So if I want to remove the delicious folder that I have now one thing that you have to keep in mind

16

00:01:12,240  -->  00:01:17,700
when calling or MTR is that the folder has to be completely empty it can't have any files or folders

17

00:01:17,700  -->  00:01:18,940
inside of it.

18

00:01:18,960  -->  00:01:24,180
So if I try to do this right now I'll get an error saying oh this is not empty and that's just kind

19

00:01:24,180  -->  00:01:31,620
of a nice safeguard in case you keep you from deleting a folder that has a ton of files and folders

20

00:01:31,620  -->  00:01:32,660
in it.

21

00:01:32,670  -->  00:01:39,810
Now if you do want to remove a folder and all of its content will have to import the SH The shell utilities

22

00:01:39,870  -->  00:01:49,440
module and shell utilities has a remove tree is kind of like the delete analog of copy tree instead

23

00:01:49,440  -->  00:01:51,980
of copying a folder and all its contents.

24

00:01:52,110  -->  00:01:56,860
This will delete a folder and all of its content.

25

00:01:57,690  -->  00:02:07,920
So if I passed it that delicious folder name I'll just scroll down here in my file explorer so my delicious

26

00:02:07,920  -->  00:02:09,830
folder has all of this.

27

00:02:09,990  -->  00:02:12,600
All of these files and folders and side of it.

28

00:02:13,220  -->  00:02:14,160
I run this code

29

00:02:18,670  -->  00:02:20,660
orange tree.

30

00:02:20,670  -->  00:02:24,270
Will delete that entire folder and everything in it.

31

00:02:24,270  -->  00:02:30,630
So you want to be really careful with these functions because these will permanently tweak these files

32

00:02:30,630  -->  00:02:31,140
and folders.

33

00:02:31,140  -->  00:02:33,640
They won't send them to the recycling bin.

34

00:02:33,660  -->  00:02:39,300
So one way that you can be really careful with this is by doing a dry run of any code that has these

35

00:02:39,300  -->  00:02:40,900
two leading functions in them.

36

00:02:40,920  -->  00:02:44,940
So let's say of a program like this it imports OS

37

00:02:47,550  -->  00:02:54,270
iterates over all of the files and folders inside the current working directory that's returned by Lister

38

00:02:55,320  -->  00:02:59,550
and I won't find any of those file names that ends with.

39

00:02:59,730  -->  00:03:08,900
And let's say I wanted to delete datea X Files but I made a typo instead and said Dot are X Files.

40

00:03:09,270  -->  00:03:15,300
But I keep writing the rest of my program and I just say I want to delete all of those 60 files here

41

00:03:16,020  -->  00:03:18,000
not realizing that I had made this typo.

42

00:03:18,120  -->  00:03:24,450
So a dry run is when you just comment out any of the code that would actually delete files where do

43

00:03:24,450  -->  00:03:33,310
any of these file operations instead have a print function call to printout that file name.

44

00:03:33,390  -->  00:03:41,620
Let's go ahead and just change the current working directory to the desktop.

45

00:03:43,050  -->  00:03:47,970
So Lister here will just return a list of strings of all the files and folders in the current working

46

00:03:47,970  -->  00:03:48,690
directory.

47

00:03:48,800  -->  00:03:52,230
I just set to desktop and I'll just save this as an example.

48

00:03:52,330  -->  00:03:53,530
Pete why.

49

00:03:53,970  -->  00:03:59,380
So since I've come it out this code I don't have anything to be worried about as far as deleting all

50

00:03:59,380  -->  00:03:59,840
the files.

51

00:03:59,850  -->  00:04:06,090
I can run this program and it'll print up all the file names of the things I thought I would normally

52

00:04:06,090  -->  00:04:10,890
be deleting so I could just check this and I realized oh wait there's this important file that's done

53

00:04:10,960  -->  00:04:18,930
or XTi that's this file right here that's on my desktop that I was going to delete.

54

00:04:18,930  -->  00:04:20,340
That's not really what I intended.

55

00:04:20,340  -->  00:04:28,590
So instead I'll go back to my program realized my mistake and then corrected then I can run this dry

56

00:04:28,590  -->  00:04:28,910
run.

57

00:04:28,920  -->  00:04:31,300
Just one more time and I can say oh yeah.

58

00:04:31,340  -->  00:04:36,330
Switching to Python that text file that's the one I wanted to delete so I could just come into this

59

00:04:36,330  -->  00:04:42,860
line and uncomment this line to actually run this program and delete this file.

60

00:04:43,110  -->  00:04:48,390
But really using these deleting functions unlink removed directory remove tree.

61

00:04:48,390  -->  00:04:54,050
Those are all kind of dangerous to use anyway because all of those deletions will be permanent.

62

00:04:54,150  -->  00:04:58,310
There's a module called Send to trash that instead of deleting it permanently.

63

00:04:58,320  -->  00:05:03,420
We'll just send it to your operating systems recycling bin so Center trash doesn't come with Python

64

00:05:03,420  -->  00:05:09,710
you'll have to install it yourself using the Pipp installer and there's instructions in the coordinates

65

00:05:09,720  -->  00:05:14,400
for how to do this for your operating system since it's different for each operating system.

66

00:05:14,460  -->  00:05:22,780
I'll just show you on Windows can open up a command line prompt and change directories to program files

67

00:05:22,780  -->  00:05:22,800
.

68

00:05:22,800  -->  00:05:29,540
Python scripts and run clips that you actually install send to trash.

69

00:05:29,640  -->  00:05:30,900
And here I've already installed it.

70

00:05:30,900  -->  00:05:35,790
So it just says so that's already installed but that's how you can just install third party modules

71

00:05:36,570  -->  00:05:38,090
using Pipp on Windows.

72

00:05:38,370  -->  00:05:47,280
So once that's installed I can just import send to trash and send to trash has a function also called

73

00:05:47,520  -->  00:05:53,240
Send to trash and then allow you to delete any files or folders that you pass it.

74

00:05:53,310  -->  00:05:58,020
But instead of doing them permanently just tweets just sends them to the recycling bin.

75

00:05:58,020  -->  00:06:05,610
So let's take that important file that are XTi that I had right here on the desktop and then pass it

76

00:06:05,910  -->  00:06:09,640
something like user's desktop.

77

00:06:10,140  -->  00:06:12,410
Important file.

78

00:06:12,700  -->  00:06:18,250
R x T.

79

00:06:18,350  -->  00:06:23,240
You can see that we did the file but not permanently.

80

00:06:23,240  -->  00:06:29,190
I can just open up my recycling bin and say oh this is that file that I needed so I could just either

81

00:06:29,520  -->  00:06:33,530
permanently delete it here or just restore it back to its original location.

82

00:06:33,960  -->  00:06:40,410
So to recap Ostara unlink will delete a file estopped remove directory will delete a folder but the

83

00:06:40,410  -->  00:06:42,310
folder has to be empty.

84

00:06:42,330  -->  00:06:43,330
SH you Tilda.

85

00:06:43,350  -->  00:06:48,590
Remove tree will delete a folder and all its contents all the files and folders inside it.

86

00:06:48,610  -->  00:06:49,840
That's kind of dangerous.

87

00:06:50,010  -->  00:06:55,160
So what you first want to do is do a dry run where you comment out all of the calls to these delete

88

00:06:55,170  -->  00:07:00,840
functions and just have a print function call instead so that you can run the program and see what exactly

89

00:07:00,840  -->  00:07:02,740
is going to do first.

90

00:07:03,390  -->  00:07:09,330
And it's probably just better to install the send to trash module and use it's send to trash function

91

00:07:09,570  -->  00:07:14,730
to delete files and folders by sending them to the recycling bin and sort of deleting them permanently

92

00:07:14,740  -->  00:07:15,240
.
