Hello everybody and welcome back to another Python basics tutorial.
And as I said in this tutorial right now we will actually cover the creation of files.
We will also learn how we can delete them write to them and read from them.
Now that is really important especially if we want to implement for example in our backdoor our download
and upload function.
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
download or to upload files to the victim P.C..
So in order to get started for this we will just create a simple 60 file since that is the most simple
file to create.
I mean we create with the same command all types of files.
But in order for you to understand it the best we will use this one.
So let us actually open up our Python key interpreter.
So right now if we import the OS command which I showed in the previous video what that means.
And we run the system function from the OS library and we Alice and see what type of files we have right
here.
You will see that we do not have any file called that's not the extent.
So let's say for example right now we want to create a file that will be called that dot the state.
Now we can do that with our single line command.
So let's make a file object first.
So the file object in order to create it.
You just type here file object underscore object or this is basically the name of your file.
You do not need to name it like this.
You can imitate anything you want.
And after that you specify equals and then open.
So basically this is the entire comment in the side of the open brackets.
You need to specify the name of the file and what you want to do with the file.
Now much want to do with the file I will cover just in a few moments but first of all let's actually
open the file name so we open up the double quotes and we type your test dot the axes since we said
we want to name the file like that.
You can name it anything you want then you specify comma and then again double quotes and then W W stands
for right.
So we will be able to write anything to a file from our python program with right.
You can actually let let's first of all just start off with right and I will show you what other mods.
You can also use.
So they are W R and A I will explain all of them in just seconds.
But first of all let's just run this comment first.
So right now we created a file object in our python programs directory
so right now if we run the always dot system my last command once again you can see that right here
we have a program or not a program.
We have a file called Test dot the extreme.
Now that file is currently empty.
We can check that with the same commands to just get a test dot the Steve.
You will see that there is nothing in the file.
And right now let's say for example we want to write our password in that file for example.
So we will do that with specifying the same name of our file that we specified in the opening comment.
So we need to specify a file object on the score object not right and then between these brackets under
the double quotes we need to specify the string that we want to add to that file so we can add anything.
So let's say password
is for example one two three four five six seven eight nine.
And we just write that to a file.
And once again if we run the course cat kept that the T.
It basically says nothing.
Let me just check out.
Why does it say that
or basically.
It should say nothing as we can see right now if I exit it and the cat that does that the next day it
will say password one two three four five six seven eight.
So good.
I didn't know I expected it to say actually something doesn't even matter let's just import the oil.
Once again let us actually now try to read from that file.
You can't use that cat command.
You can actually use file object dot read in order to actually read that file.
But right now since I closed my interpreter I actually have to open that file once again since I closed
first my interpreter.
Ten seconds ago right now reopened it my Python interpreter doesn't really know what file object is.
Now let me just show you right now you can name it again anything you want.
So file equals open and as you can see right now I can open a file that already exists.
So basically what this will do is it will check out if the file exists and if it does it will open it
for us.
And if the file doesn't exist it will create a new file for us called the test not the extreme but it's
in our case right now.
The file exists and in that file we have the passwords or a single password that we specified with our
dot right command.
Right now it will open that file and we did that file.
It will ask us in the next specification what do we want to do with it.
Right now we want to specify our right here which basically stands for read.
So what we want to do we want to read from that file we want to read what the contents of the file so
close the double quotes closed the brackets.
And right now if you just type your file dot read and then open the brackets and specify nothing and
then you will see that the content of our file is basically passport and then these two dots and then
one two three four five six seven eight nine which is specified in the previous open Python interpreter.
So
right now we know that we can open a file we can write to it.
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
since you didn't open the file for writing.
You only open the file for reading.
Now if you were to open the file for writing once again so let me just show you.
Let's go to the back comment and type.
Right right here.
You can also perform the or basically since you did not open the file since you will reopen the file
once again for writing.
Right now you cannot read from that file since it says file not open for reading.
But right now you can run this command that we tried before which is file dot right.
One two three.
If you just specified that.
And right now if you open file once again for reading which is just ah and you tried to read that file
you will see right now that file only has one two three and it doesn't have the file or pardon me the
password.
And then one two three four five six seven eight nine in it so it's good to understand that R stands
for read W stands for right and a which you can specify right right here.
So if you open for a plus it will actually allow you to append to that file as you saw previously.
Once we open the file to rights to it it's actually first deleted all of the previous contents of that
file and then written the 1 2 3 to it.
It basically replaced the previous contents with this right here.
Right now let's say for example if we wanted to add four five six to this without deleting the one two
three we would have to open that file with a plus.
So you open that file as though the DB and then you specify D a plus right here and right now if you
just type your file that's right and it specify for four or five six and then you try to read from that
file.
Whoops.
Why does it say nothing
right now it this says nothing.
So not really sure why that happens.
Let me just close this once again and catch it as the extreme.
OK.
So you have right now.
Not really sure why sometimes it can.
It shows and sometimes it doesn't.
Probably because I opened it in the append mode.
That's why the read didn't work.
But right now we have the one two three four five six and four five six.
Now we have twice the four five six since I actually typed it twice in order to check why it couldn't
read.
But you can see that it successfully appended these other numbers to our 1 2 3.
So let's actually write a program.
So now let's remove the tester to sleep so we can make a new test to Steve.
That's actually a nano Python.
A dot pi.
So right now standard the input our Python path and right here we can for example open our file so a
fan equals open let's say it's called I know.
Let's call it the contest not the extreme we open it.
Write it then we close this.
Then after that we actually want to write to that file so if I find out right.
We after that need to write what we want to write so let's say we want tell a world.
Hello world.
Right here we close that
and then we can actually more let's actually leave it for a second just on this.
So let's control all save control acts to exit stage mode by 28 point.
Point by and then by template we run it you will see nothing really happens but if you compare this
illustrate here and this illustrate here you will see that right now we have to test out the file with
the hello world and let's say we want to make another program but we will use the same one we will just
re write this one and we want to read from a file.
We will specify this right here.
We will specify dot read.
So and read the basically not really sure if we have to print this.
I don't think so.
So let's save this.
Now this will open this file right here and it will print out all the contents that are currently in
that file.
Now in my case that is the Heller world so that's actually run that once again.
So we have to we have to add the print statement.
OK.
Doesn't matter let's edit.
So print f and read and read.
Now it should print the hello world to us as we can see right here.
It's successfully printed the contents of a file in our output.
So it's good to remember that in the W stands for right read stands for.
Pardon me stands for read and a stands for append.
So that's something you should know.
It is very useful and we will be using these commands in our backdoor making so that's about it for
this tutorial.
I hope you did and I hope I see you in the next one by.