1 00:00:00,210 --> 00:00:04,950 Hello everybody and welcome back to another Python basics tutorial. 2 00:00:04,950 --> 00:00:10,010 And as I said in this tutorial right now we will actually cover the creation of files. 3 00:00:10,020 --> 00:00:14,000 We will also learn how we can delete them write to them and read from them. 4 00:00:14,070 --> 00:00:19,710 Now that is really important especially if we want to implement for example in our backdoor our download 5 00:00:19,770 --> 00:00:21,100 and upload function. 6 00:00:21,180 --> 00:00:26,100 We want to be able to download all of the files from the victim's P.C. and we also want to be able to 7 00:00:26,100 --> 00:00:29,350 download or to upload files to the victim P.C.. 8 00:00:29,670 --> 00:00:37,110 So in order to get started for this we will just create a simple 60 file since that is the most simple 9 00:00:37,110 --> 00:00:38,040 file to create. 10 00:00:38,040 --> 00:00:41,360 I mean we create with the same command all types of files. 11 00:00:41,370 --> 00:00:45,420 But in order for you to understand it the best we will use this one. 12 00:00:45,900 --> 00:00:50,620 So let us actually open up our Python key interpreter. 13 00:00:50,670 --> 00:00:57,840 So right now if we import the OS command which I showed in the previous video what that means. 14 00:00:57,840 --> 00:01:04,680 And we run the system function from the OS library and we Alice and see what type of files we have right 15 00:01:04,680 --> 00:01:05,400 here. 16 00:01:05,400 --> 00:01:09,800 You will see that we do not have any file called that's not the extent. 17 00:01:09,840 --> 00:01:17,080 So let's say for example right now we want to create a file that will be called that dot the state. 18 00:01:17,100 --> 00:01:20,910 Now we can do that with our single line command. 19 00:01:20,910 --> 00:01:23,330 So let's make a file object first. 20 00:01:23,340 --> 00:01:25,160 So the file object in order to create it. 21 00:01:25,170 --> 00:01:30,500 You just type here file object underscore object or this is basically the name of your file. 22 00:01:30,570 --> 00:01:32,970 You do not need to name it like this. 23 00:01:32,970 --> 00:01:34,710 You can imitate anything you want. 24 00:01:34,710 --> 00:01:39,570 And after that you specify equals and then open. 25 00:01:39,900 --> 00:01:43,990 So basically this is the entire comment in the side of the open brackets. 26 00:01:44,010 --> 00:01:49,030 You need to specify the name of the file and what you want to do with the file. 27 00:01:49,050 --> 00:01:55,530 Now much want to do with the file I will cover just in a few moments but first of all let's actually 28 00:01:55,650 --> 00:02:03,360 open the file name so we open up the double quotes and we type your test dot the axes since we said 29 00:02:03,360 --> 00:02:04,770 we want to name the file like that. 30 00:02:04,770 --> 00:02:12,180 You can name it anything you want then you specify comma and then again double quotes and then W W stands 31 00:02:12,180 --> 00:02:12,750 for right. 32 00:02:12,750 --> 00:02:18,770 So we will be able to write anything to a file from our python program with right. 33 00:02:18,780 --> 00:02:25,160 You can actually let let's first of all just start off with right and I will show you what other mods. 34 00:02:25,170 --> 00:02:26,410 You can also use. 35 00:02:26,550 --> 00:02:31,560 So they are W R and A I will explain all of them in just seconds. 36 00:02:31,580 --> 00:02:34,620 But first of all let's just run this comment first. 37 00:02:34,640 --> 00:02:39,930 So right now we created a file object in our python programs directory 38 00:02:45,400 --> 00:02:51,260 so right now if we run the always dot system my last command once again you can see that right here 39 00:02:51,620 --> 00:02:54,360 we have a program or not a program. 40 00:02:54,380 --> 00:02:57,320 We have a file called Test dot the extreme. 41 00:02:57,620 --> 00:02:59,950 Now that file is currently empty. 42 00:03:00,110 --> 00:03:06,000 We can check that with the same commands to just get a test dot the Steve. 43 00:03:06,260 --> 00:03:08,400 You will see that there is nothing in the file. 44 00:03:08,570 --> 00:03:13,790 And right now let's say for example we want to write our password in that file for example. 45 00:03:13,820 --> 00:03:19,550 So we will do that with specifying the same name of our file that we specified in the opening comment. 46 00:03:19,550 --> 00:03:26,930 So we need to specify a file object on the score object not right and then between these brackets under 47 00:03:26,930 --> 00:03:32,320 the double quotes we need to specify the string that we want to add to that file so we can add anything. 48 00:03:32,320 --> 00:03:34,490 So let's say password 49 00:03:37,970 --> 00:03:41,460 is for example one two three four five six seven eight nine. 50 00:03:42,500 --> 00:03:44,660 And we just write that to a file. 51 00:03:44,660 --> 00:03:48,710 And once again if we run the course cat kept that the T. 52 00:03:49,040 --> 00:03:52,320 It basically says nothing. 53 00:03:52,490 --> 00:03:53,770 Let me just check out. 54 00:03:53,810 --> 00:03:55,010 Why does it say that 55 00:03:59,130 --> 00:04:00,010 or basically. 56 00:04:00,070 --> 00:04:04,480 It should say nothing as we can see right now if I exit it and the cat that does that the next day it 57 00:04:04,480 --> 00:04:06,400 will say password one two three four five six seven eight. 58 00:04:06,430 --> 00:04:07,450 So good. 59 00:04:07,450 --> 00:04:13,090 I didn't know I expected it to say actually something doesn't even matter let's just import the oil. 60 00:04:13,090 --> 00:04:17,530 Once again let us actually now try to read from that file. 61 00:04:17,620 --> 00:04:19,300 You can't use that cat command. 62 00:04:19,300 --> 00:04:24,650 You can actually use file object dot read in order to actually read that file. 63 00:04:24,730 --> 00:04:33,220 But right now since I closed my interpreter I actually have to open that file once again since I closed 64 00:04:33,340 --> 00:04:35,200 first my interpreter. 65 00:04:35,230 --> 00:04:42,190 Ten seconds ago right now reopened it my Python interpreter doesn't really know what file object is. 66 00:04:42,190 --> 00:04:46,090 Now let me just show you right now you can name it again anything you want. 67 00:04:46,090 --> 00:04:53,560 So file equals open and as you can see right now I can open a file that already exists. 68 00:04:53,560 --> 00:04:58,750 So basically what this will do is it will check out if the file exists and if it does it will open it 69 00:04:58,750 --> 00:04:59,480 for us. 70 00:04:59,560 --> 00:05:05,290 And if the file doesn't exist it will create a new file for us called the test not the extreme but it's 71 00:05:05,290 --> 00:05:06,400 in our case right now. 72 00:05:06,400 --> 00:05:13,050 The file exists and in that file we have the passwords or a single password that we specified with our 73 00:05:13,120 --> 00:05:14,640 dot right command. 74 00:05:14,650 --> 00:05:19,470 Right now it will open that file and we did that file. 75 00:05:19,490 --> 00:05:23,420 It will ask us in the next specification what do we want to do with it. 76 00:05:23,440 --> 00:05:27,850 Right now we want to specify our right here which basically stands for read. 77 00:05:27,860 --> 00:05:34,130 So what we want to do we want to read from that file we want to read what the contents of the file so 78 00:05:34,700 --> 00:05:37,160 close the double quotes closed the brackets. 79 00:05:37,160 --> 00:05:43,310 And right now if you just type your file dot read and then open the brackets and specify nothing and 80 00:05:43,310 --> 00:05:48,860 then you will see that the content of our file is basically passport and then these two dots and then 81 00:05:48,860 --> 00:05:56,330 one two three four five six seven eight nine which is specified in the previous open Python interpreter. 82 00:05:56,570 --> 00:05:56,960 So 83 00:06:00,140 --> 00:06:03,800 right now we know that we can open a file we can write to it. 84 00:06:03,890 --> 00:06:10,760 So right now if you for example tried to write to it you wouldn't be able to since let's say 1 to 3 85 00:06:12,440 --> 00:06:15,200 since you didn't open the file for writing. 86 00:06:15,200 --> 00:06:17,810 You only open the file for reading. 87 00:06:18,330 --> 00:06:24,600 Now if you were to open the file for writing once again so let me just show you. 88 00:06:24,680 --> 00:06:27,400 Let's go to the back comment and type. 89 00:06:27,440 --> 00:06:29,030 Right right here. 90 00:06:29,240 --> 00:06:37,790 You can also perform the or basically since you did not open the file since you will reopen the file 91 00:06:37,790 --> 00:06:39,240 once again for writing. 92 00:06:39,300 --> 00:06:44,870 Right now you cannot read from that file since it says file not open for reading. 93 00:06:44,870 --> 00:06:48,380 But right now you can run this command that we tried before which is file dot right. 94 00:06:48,380 --> 00:06:49,270 One two three. 95 00:06:49,880 --> 00:06:51,560 If you just specified that. 96 00:06:51,780 --> 00:07:00,200 And right now if you open file once again for reading which is just ah and you tried to read that file 97 00:07:00,200 --> 00:07:06,250 you will see right now that file only has one two three and it doesn't have the file or pardon me the 98 00:07:06,380 --> 00:07:07,160 password. 99 00:07:07,160 --> 00:07:14,020 And then one two three four five six seven eight nine in it so it's good to understand that R stands 100 00:07:14,020 --> 00:07:18,570 for read W stands for right and a which you can specify right right here. 101 00:07:18,570 --> 00:07:25,920 So if you open for a plus it will actually allow you to append to that file as you saw previously. 102 00:07:25,920 --> 00:07:31,650 Once we open the file to rights to it it's actually first deleted all of the previous contents of that 103 00:07:31,650 --> 00:07:34,120 file and then written the 1 2 3 to it. 104 00:07:34,470 --> 00:07:38,760 It basically replaced the previous contents with this right here. 105 00:07:38,760 --> 00:07:43,830 Right now let's say for example if we wanted to add four five six to this without deleting the one two 106 00:07:43,830 --> 00:07:47,730 three we would have to open that file with a plus. 107 00:07:47,730 --> 00:07:57,170 So you open that file as though the DB and then you specify D a plus right here and right now if you 108 00:07:57,170 --> 00:08:05,850 just type your file that's right and it specify for four or five six and then you try to read from that 109 00:08:05,850 --> 00:08:07,130 file. 110 00:08:08,550 --> 00:08:09,370 Whoops. 111 00:08:09,450 --> 00:08:10,790 Why does it say nothing 112 00:08:15,390 --> 00:08:16,950 right now it this says nothing. 113 00:08:16,950 --> 00:08:19,200 So not really sure why that happens. 114 00:08:19,200 --> 00:08:22,740 Let me just close this once again and catch it as the extreme. 115 00:08:23,420 --> 00:08:23,720 OK. 116 00:08:23,730 --> 00:08:24,750 So you have right now. 117 00:08:24,780 --> 00:08:26,270 Not really sure why sometimes it can. 118 00:08:26,310 --> 00:08:28,500 It shows and sometimes it doesn't. 119 00:08:28,530 --> 00:08:32,310 Probably because I opened it in the append mode. 120 00:08:32,310 --> 00:08:33,560 That's why the read didn't work. 121 00:08:33,600 --> 00:08:38,260 But right now we have the one two three four five six and four five six. 122 00:08:38,280 --> 00:08:45,110 Now we have twice the four five six since I actually typed it twice in order to check why it couldn't 123 00:08:45,110 --> 00:08:45,750 read. 124 00:08:45,990 --> 00:08:53,410 But you can see that it successfully appended these other numbers to our 1 2 3. 125 00:08:53,420 --> 00:08:56,740 So let's actually write a program. 126 00:08:56,750 --> 00:09:03,430 So now let's remove the tester to sleep so we can make a new test to Steve. 127 00:09:03,810 --> 00:09:05,470 That's actually a nano Python. 128 00:09:05,930 --> 00:09:06,900 A dot pi. 129 00:09:07,770 --> 00:09:17,930 So right now standard the input our Python path and right here we can for example open our file so a 130 00:09:17,990 --> 00:09:26,140 fan equals open let's say it's called I know. 131 00:09:26,160 --> 00:09:30,380 Let's call it the contest not the extreme we open it. 132 00:09:30,430 --> 00:09:34,480 Write it then we close this. 133 00:09:34,680 --> 00:09:39,570 Then after that we actually want to write to that file so if I find out right. 134 00:09:40,830 --> 00:09:46,020 We after that need to write what we want to write so let's say we want tell a world. 135 00:09:46,140 --> 00:09:46,640 Hello world. 136 00:09:46,640 --> 00:09:48,890 Right here we close that 137 00:09:53,420 --> 00:09:59,000 and then we can actually more let's actually leave it for a second just on this. 138 00:09:59,000 --> 00:10:06,750 So let's control all save control acts to exit stage mode by 28 point. 139 00:10:07,070 --> 00:10:14,230 Point by and then by template we run it you will see nothing really happens but if you compare this 140 00:10:14,330 --> 00:10:21,830 illustrate here and this illustrate here you will see that right now we have to test out the file with 141 00:10:21,830 --> 00:10:29,300 the hello world and let's say we want to make another program but we will use the same one we will just 142 00:10:29,510 --> 00:10:33,280 re write this one and we want to read from a file. 143 00:10:33,680 --> 00:10:35,570 We will specify this right here. 144 00:10:35,630 --> 00:10:37,450 We will specify dot read. 145 00:10:37,460 --> 00:10:43,280 So and read the basically not really sure if we have to print this. 146 00:10:43,280 --> 00:10:44,520 I don't think so. 147 00:10:44,540 --> 00:10:45,920 So let's save this. 148 00:10:45,950 --> 00:10:51,590 Now this will open this file right here and it will print out all the contents that are currently in 149 00:10:51,590 --> 00:10:52,510 that file. 150 00:10:52,520 --> 00:10:58,250 Now in my case that is the Heller world so that's actually run that once again. 151 00:10:58,280 --> 00:11:00,970 So we have to we have to add the print statement. 152 00:11:01,160 --> 00:11:01,740 OK. 153 00:11:01,760 --> 00:11:04,540 Doesn't matter let's edit. 154 00:11:04,550 --> 00:11:06,740 So print f and read and read. 155 00:11:06,740 --> 00:11:12,500 Now it should print the hello world to us as we can see right here. 156 00:11:12,640 --> 00:11:17,020 It's successfully printed the contents of a file in our output. 157 00:11:17,020 --> 00:11:24,690 So it's good to remember that in the W stands for right read stands for. 158 00:11:24,750 --> 00:11:28,920 Pardon me stands for read and a stands for append. 159 00:11:29,010 --> 00:11:31,040 So that's something you should know. 160 00:11:31,200 --> 00:11:37,950 It is very useful and we will be using these commands in our backdoor making so that's about it for 161 00:11:37,950 --> 00:11:39,090 this tutorial. 162 00:11:39,150 --> 00:11:42,720 I hope you did and I hope I see you in the next one by.