Now, let us understand how the program works.
Now here we will, first of all, see working with the files in Python.
Now, before we dive into the code for our file, that is Demo 17 or Beevor, we will first of all examine
how Python works with files.
So the three steps to reading the content of your file are Faustus opening the file, then reading the
content or the file content into a normal variable and finally closing the file.
So similarly, to write a new content in a file.
We must open or create the file, write the new content and again, close device.
So coming to opening of Right Now in Python, we can open a file to read from our right to using the
open function.
The open function.
First parameter is the name of the file to open.
If it is the file in the same folder as the current Python program that you're typing, then you just
give the file named in the command to open the file.
If it existed in the same folder as your Python folder would basically look like if you give, like
here, the file object.
And we are giving your defined name and then we seeing the final object, SDR is equal to open and now
a final object is basically stored in a file object variable, which will be used to read from or write
to the fight.
You can also specify the absolute path of a particular file, which includes the folder and the parent
folders that the file is in.
For example, if it is something like, say, in E drive user folder, slash, python slash ABCDE or
so, you'll have to give this equal number slash user's double slash.
Then between the West and the file name DOT, the 60 is actually your absolute back.
So remember that on Windows the backslash must be escaped by typing another backslash before it.
Now suppose all you want to pass the part of the file as a string for the open functions.
First parameter, then you can directly see or instead of giving the object name, he can give open
in bracket the part of the fight.
The file object has several methods for reading to or writing to, reading from and closing the file.
So coming to the writing to end the closing of files, not for the encryption program.
After reading the text file content, you will need to write the encrypted or decrypted content to a
new file, which you would be doing using the right method.
So to use the right method on the file object, you need to open the file object in the right mood,
which you do by passing open the string W as the second parameter like we have done over here.
Now this second parameter of the argument is an optional parameter because open function can still be
used without passing two arguments.
Now, like here, we have given w this creates a file named the output file name a star which is having
defined them for the output file is in the right mode so you can edit it.
So if a file of the same name exist where the open function creates a new file, the old file would
be overridden.
So be careful when using the open in the right now using the particular file.
Now in the opening right, you can write to the file by calling the right material and the right method
takes one argument, a string of text which you want to write to the file so you can enter something
in the interactive shell.
For example.
C we go back here and the fourth thing that we do here is we will open the file.
So let's take the file object SDR that's equal to the open one file.
See A, B, C, dot the.
OK, now we have to first create this file, so what we'll have to do is we'll have to go to this folder
and let us create your file, see ABC be extinct.
And now here we will see File Obejas Star Dot Open.
We will give your ABC and we want to open it in writable.
So rather, we will say, is equal to all, the first thing is we would give them the open command that
is our final object name is equal to open.
And now once we have open the particular file, we say file object is our DOT Veals right method we
bring to your say hello, Baynton.
File object.
Repressed and now suppose you go back and try to open ABC, we have not yet closed the particular fight.
So what we will do is file object, STF dot close first and then we will open ABC.
You get there.
Hello, Biton file object.
Prenter So when you pass the string to the right matau it right that string to the text file and prints
a particular number.
That is a number of characters in the string return to the file and when you're finished working with
the file you need to tell Biton that it is done with the by calling the function clause method on the
file object as we have done.
So there is also an update on the mode, which is like write more except append mode doesn't override
the file.
So instead strings are written to the end of the content already in the file.
Although we don't use this in this particular program, you can open a file in the append would also
by passing a string E as the second argument to the open function.
So if you get some error like I ordered unsupported operation or not readable error message, then when
you're trying to call the write on a file object, you might not have open the file in the right more.
So when you don't include the open functions optional parameter, it automatically opens the file in
read mode that is are instead, which allows you to only use the reading matter on the file object not
writing.
So let us now see reading the data from the file instead of going in the file directly over here from
Python, we can use the reading matter, which returns a string containing all the text in the fight.
Not to try out that we will read the same ABC file which we have created earlier with the right method.
So now for that we will take the file object as the R is equal to again.
We would give open A, B, C, dot the city.
Now we are giving us even if we don't give out by default, it is in the readable.
But still we are specifying that we want to give out.
Then we give your content as the R is equal to we give file object SDR don't use the read method and
store the values in the given variables.
Then we say print the value of content SDR here, which would print whatever string you have in the
particular fight.
And once you have done, don't forget to again close the file object.
So your file is open and the file object that is created is stored in the file object SDR variable.
So once you have the file object with you, you can read the file using the agreed method and stored
in it.
The content is still variable, which you then print for printing the values in the file.
Now if you get some error messages like IO error or error number two or no such file or directories,
then then make sure that the file actually is where you think it is.
And double click or double check that name that you have typed on the folder name also, or if it is
in a proper direct result of the directories, another vote or a particular folder.
Now we would be using this open read right close on our file that we have opened to encrypt or decrypt
in our demo that we have created.
So now let's go back to our Demo 17, 1735, and let us see setting up our main function.
Now, the first part of this program should look familiar.
We have an import statement which is importing our data.
That is the time object.
If we look at the full statement here, then we are importing the OS.
This is the demo tape and the demo 13 parameters.
Now, then we start with other men by setting up some variables to use in our program.
That is our input file.
Name a star variable which holds a string of a file to read and the encrypted or file or the decrypted
text is returned to the file name in the output file name is still.