[no audio]
Friends, using Python you can work with like text files, JSON
files, CSV files, HTML files, and so on, and as of now here we
are going to work with text files.
So simply working with text files using Python.
So guys before going to work with your text files using Python
let's move into our general GUI procedure whenever if
you want to create or read the content from your text file,
right. See what I am doing is, suppose I want to create a
new file, right,
yeah, before that see guys, generally if you want to work with
text files, right, you have three options.
That is you can create a new file,
or add content to an existing file, or just for reading purpose
read a file content.
Right. See, whenever if you are going with GUI procedure,
I mean in this way, 'Right Click', 'New', then your file name,
I mean, let me select my file as text file.
I mean, I want to create file as text.
Right. I'm creating,
let's say file name as some 'demo.txt', right. Now
I am going to open it. Observe that.
I am going to open it. Open it.
Yes, I opened. Right. See guys, in your GUI procedure
whenever if you open your file, you can read your file,
you can write content to your file, right, all the options
are available simply by opening your file in GUI mode, but
command line, or through programming, or through scripting,
it's not possible.
You have to open your file either to write content, or to
read content, or to add some new data to the existing file.
Be clear. See in GUI mode it is possible simply by opening your
file, but whereas through programming if you want to work
with your files, right, you have to open your file,
you have to open your file either in 'write' mode, means if
you want to create a new file, right, or you have to open
your file in 'append' mode, means adding new content to existing
file, then you have last option, that is 'read' mode, just to
read. Right. See that. You can open your file -
If you want to create a new file
you have to open your file with the 'w' mode,
or if you have already a file and if you want to add, append
data to an existing file, then you have to open your file
in 'append' mode,
or simply if you want to read content from your file, you
have to open it using 'r' mode. Right. Fine.
And one more thing guys, once if you write some content to your
file, right, see I'm opening, steps are important,
I am opening, based on cursor position
you are going to write content. Suppose if cursor is somewhere
there, I am moving cursor to some place, then I can write.
So wherever your cursor is there, based on that cursor only
you are writing content to your file.
[no audio]
Fine. So assume that I am writing, "This is a demo file",
and I am going to close it, while closing it is asking 'Save'
option. But in programming in your Python Script, once if you
close, automatically the data will save into your file.
You don't need to provide confirmation like save or don't
save. Whenever if you close, automatically your data will store
into your file. Right. See now
[no audio]
I'm going to, see guys, whether you want to read a content,
or whether you want to write a content to your file always you
should get in your programming first your cursor position,
or your file object.
You can take any variable.
I am taking 'fo', file object. Just assume that this is like
your cursor position.
So once if you get into your hand, cursor position, then after
that whether you want to read or write you can do it.
So now I want to get my cursor, right, to write content to
your file. Then open your file in 'write' mode.
Open your file in 'write' mode.
Let's say 'newfile.txt'.
I'm writing, then 'w' mode, 'write' mode.
Then after that simply you can close.
So as of now, what I am doing is simply I'm opening a file
and then closing a file. While opening, right,
I'm giving some file name. Automatically this file will be created
with empty data.
Now this is your cursor position. With cursor position
nowhere I am writing any data to your file.
Simply I'm closing.
It's like creating an empty file, right?
Let me check it.
So this is simply
[no audio]
'Creation of empty file'. That's it.
Let me open editor.
So I am going to open my, you can take a Sublime or Atom editor,
whatever it maybe you can take. I'm going to suppose, Sublime Text.
Right. Let's open that first.
[no audio]
Let me create a file.
So guys, first of all,
right, so, "Working with
[no audio]
text files". You know to work with your
text files you have different modes - write, read or append.
First of all because we are going to create a new file,
assume that we don't have any files to read,
we need to create some file, right?
So before going to create always in your hand you should
have a cursor position from your file.
That's why what I am doing is, first I'm going to open my
file. Let's say some file, right, some 'newdemo.txt' file
I am creating. So I want to open this in 'write' mode.
So now this file I am opening for 'write' mode.
So once you've opened your file automatically this syntax is going
to give your cursor object, simply file object. That's it.
Now based on this object
you can write some content to your file.
But what I am doing is, simply I opened my file, and then closed.
Right. Let me save it. Be clear.
[no audio]
Let me save it with some name, right.
So I'm going to store on my Desktop.
[no audio]
'create_a_file.py'. That's it.
Now this is a Python Script.
Now, what I am doing is, I am going to run our code. Just simply
'Ctrl + B'
[no audio]
Done. Done.
You don't have any output here because you don't have any
'print' statement to get your output, right.
Simply we created a file.
Let me open our folder.
[no audio]
'create_a_file', just now we created this. Let me, sorry,
that is a Python Script,
right. So our file name is 'newdemo'.
[no audio]
Yeah, see here, 'newdemo.txt'. Open this. You don't have
any content because you just opened and closed your file, okay.
Okay, we'll write our data to your file,
but before that suppose I want to see in which mode your
file is opened.
You can check it simply 'print(fo.mode)'.
So it is going to give you the mode of your file 'write'
mode. Right. Then,
let me comment this. Now,
you can check it, 'fo.readable'.
Observe the output, what you are getting. Yeah, this is a function.
Let me run it, 'False'
you are getting. That means you opened your file not for read,
some other thing, maybe write mode or append mode. That we
have seen by using 'fo.mode'.
So 'fo.readable', it is going to give 'False' means you
can't read the content if you open your file with 'w' mode, that's it.
Let me print one more thing, 'fo.
writable'. Yes, 'True'
you're getting. Yes,
we can write some content to this file.
Actually, no need to check this.
Yes, there are some options
that's why I am giving this. Right.
Now, let me comment this entire code.
[no audio]
Be clear. Now,
what I am doing guys? I want to write a file. Then first open
with some name, random name,
you can take anything, right?
I am taking suppose 'random.txt'.
So you want to write to a file at very first time, you don't
have any file previously, assume that. Then open it in 'write'
mode. So whenever if you open with this syntax automatically
your Python will give your file object.
Assume that there is like a cursor
in your Notepad.
Yes, now in your hand you have a cursor position, then what I want
to do is I want to write some content. Then based on your
cursor position just '.write' operation.
Let me write, "This is a first line", I am writing.
Then after that close it.
So guys what we did is just we have written three lines of
code to create a file and to write some content into that
file. Now if you run this code then only your file will create.
You have written a Python Script just to create your file
with content of, "This is a first line".
Right. Let me save it and run it. It's done.
Right. Let me go to here. Somewhere
you will have 'random'. See that.
Let me open this file.
"This is a first line".
Guys, what I am doing is,
I'm going to write one more line. 'write("
This is a second line").
Let me save it and run it.
[no audio]
Open your file and see the content.
"This is a first line".
"This is a second line".
You're getting both the lines in the same line in your file.
But actually that is not our requirement.
We need to get, "This is a first line" in one line,
"This is a second line" in next line. But whenever if you want
to, whenever if you want to send the next line into next line
at the end of each and every line you have to write '\n'
so that your cursor will go to next line.
I mean generally you know, suppose assume that you are going
to write some content in any file, right,
so what I'm doing is manually I'm typing,
[no audio]
"This is a line".
Guys, if I want to write content to next line what I am doing,
I am entering my 'Enter' key on my keyboard, right, then only
your cursor is going to next line.
That's what I am doing here with help of '\n'. For last line,
suppose if this is last line you don't need to send your cursor
to the next line. That's it.
Let me save it and run it. So before run let me close this
file. Yeah, let me run it.
Now open your file, check the content. Now you have, "This is
a first line" in one line.
"This is a second line" in next line, right.
So automatically your cursor is sending to next line. Suppose
if I want to write one more line, 'write(, then, "This is a third
line")'. I am writing. So guys not only here, before writing your
third line also you can send your cursor to the next line. '\n' means
simply your cursor will go to next line. In next line
this content will write but providing '\n'
at very first line is not a good practice,
that's why always we mention at the end of your content
'\n'. You need to remember this one.
Let me save it and run it.
[no audio]
Sorry, where is your file? Here, 'random'. Open it and see the
content. You have three lines. And one more thing guys,
it's very important.
I am running this code again
and again. Let me run it
once, I run second time, third time. Three times I run this
script. But if you observe your content, right, if you observe
your content, it is only three lines. So you can feel, I mean
you are running your code three times then maybe your data
will be there three times
in your file, but it is not there in your file
three times, it
is there only one time. See you are not getting like this,
no, you're running your program three times,
then you're not getting your file in this way.
Your data is not repeating three times in your file.
It won't happen.
See what actually it is doing, whenever if you open your file
in write mode,
it's very important,
whenever if you open your file in write mode, if file is already
there, then first of all your Python will simply remove
this file and then same file name will be created.
If file is not there, new file is going to be created, or if file is not there
it will create a new file, if file is already there your content
we will overwrite, your content will overwrite. See that, suppose
I am going to write here,
[no audio]
let me delete these two lines.
[no audio]
So what I am doing is,
[no audio]
"This is a new content".
[no audio]
Let me save it and run it. Done. Right. Now,
see the content.
[no audio]
Your previous data is gone.
Only new content is there. Previously you have three lines,
right? But now you have only one line.
The reason is, whenever if you open your file in 'write' mode
simply you can assume that, if already file is there with
this name that file we'll delete and new, with the same name
new file we'll create. If file is not there, first time
you're going to create that file. That's it.
[no audio]
Right. Fine.
So guys here you are writing your content to your file one by one.
I mean, suppose if I want to write three lines how you are
going to write? Three times you are using 'fo.write',
'fo.write', 'fo.write'.
No need of that. See what I am doing is, I am going to comment
these three lines. Instead of that, instead of that
[no audio]
'fo.writelines', and here you have to provide your
data as a list.
[no audio]
You can also store your data somewhere
as a list, let's say 'my_content',
[no audio]
that we have to take as a list. In that list each and every
value is one line.
Let's say, "This is a data - 1", suppose.
So at the end you have to add '\n', right.
So if you don't add you can also take care while writing
your content to your file with the help of loop.
I will show you that. Meanwhile, first to get basic
idea I am giving in this way.
"This is a data - 2".
Then, "This is a data - 3", suppose.
Right. See if I don't add here '\n', what will happen?
Let me save it.
See guys, instead of writing line by line with the help
of 'fo.write' function, right, 'fo.write' method,
what we are doing is, 'fo.writelines'. Here
you can provide your content as a list. Now
our content is a list. Somewhere we have our list of content
here. Let me save it and run it. Same file name 'random'.
Previously you have this information, right. Now,
what we did? We opened that file in 'w' mode.
That means, if there is any file with the same name that content
will delete or that file will delete and new content
we will write to this file. Now
let me go here and open your file. See that.
"This is a data - 1".
"This is a data - 2". "This is a data - 3". In case if you want to
send "This is a data - 3" to next line, what you have to do? At
the end of your second line
you have to write '\n'.
Right. Fine. But providing '\n' at
the end of each and every line it's not a good practice. Automatically
you need to add '\n' for each and every line, right?
So for that what I am doing is, now let me comment all these things.
[no audio]
Give me a second.
Now I am going to write the different file name.
I don't want you same file name.
Of course, you can use it, no problem.
Let me take first of all 'my_content' as something like
[no audio]
"This is using loop".
Let me take, "- iteration- 1".
[no audio]
Then,
"This is using loop - iteration- 2".
Let me copy this data one more time.
[no audio]
Right. See that.
So what I am doing is, this is a text, right,
so let me take this as 3. Now,
first of all guys,
first of all I can take one by one value from your list
using for loop, right?
If you remember 'for each_line', let me take, you can take any
variable here 'for each_line in my_content'.
Right. See what I am doing, 'print(each_line)'.
Save it. Run it. You're getting one by one line.
Now before printing your line, at the end
I am adding '\n'. You have no problem. You can add it no. Yes.
Now, what I am doing is I want to create a file with this
content, whatever the content you have in your list.
So I want to create a file called,
let's say, 'with_loop.txt'.
I want to create this file.
So open in 'write' mode.
So it is going to give a file object.
Actually, that is a class,
okay. As of now, you can't understand that.
Just assume that 'fo' is a cursor. Just for your understanding
purpose I am giving 'fo' as a cursor guys, right. If you assume
that it is going to work the same operation whatever your
cursor is doing in GUI mode.
So just now we got into our hand cursor position of this file.
Now what we have to do,
we want to write line by line content to your 'with_loop.txt' file.
See, you know, if you have one single line how you're going
to write your data, 'fo.write'. That's it.
See what I am doing is, automatically I am taking line by
line from your list, and I am writing that line to your file
with the help of 'fo.write' method, and then while writing
at the end of each and every line I am adding '\n'. Now
no need to manually write '\n' at the end of each
and every line, right?
Actually, this is not a practice, good practice because you are going
to get your data from somewhere, there maybe number of
lines, right?
So logically you need to '\n' at the end of your each
and every line while adding that data to your file, that's it.
Right. So after this what you have to do? You completed your for
loop suppose. For three iterations
this loop is going to over. Then after loop
don't forget to close your file.
Once if you close your file, automatically the data will store,
will save into your file called 'with_loop.txt' file.
Let me save it and run it.
Done. Now go to here. Somewhere see 'with_loop'
is there. Open that. See that.
[no audio]
Right. Fine.
See guys, suppose if you have some file somewhere, old file
is there, and you don't want to disturb the existing data,
and you want to append the new data to your file then nothing
is there. Simply open your file in mode, 'a', 'append'. At the end
you are going to add new data.
[no audio]
Right. Let me save it and run it once. See guys, previously you
have the content in your file, three lines content.
Now, what I am doing is I am opening my file in 'append' mode. Whenever
if you open your file in 'append' mode if this file is existing,
it won't disturb the existing data.
It will keep the data as it is. Then the new data will be appended
at the end of your file.
So same content
I want to add once again to my file.
Yes, just open in 'append' mode, right. See that. I'm running.
It's done. Now open your file.
[no audio]
See that. Your data is there two times. Once again, you can
run, so that once again the same data you're going to append
at the end of your file. Now three more extra lines.
Let me open that file.
Yes. Right. Fine. Now assume that we don't have this file.
Let me remove this file.
[no audio]
Guys, whenever if you try to append a data to this file, if
it is there then the new data will be appended at the end. If
it is not there, no problem
your Python will create a new file now.
So if file is not there then 'append' mode and 'w' mode both are same.
If file is not there 'append' mode and then 'write' mode both
are same. Now
let me save it and run it.
I am running. It's done without any errors means, it worked
perfectly. Let me open your file.
See now you've got 'with_loop' file, three lines of content. Now,
I am going to rerun.
[no audio]
So already 3 lines of information is
there no, because of this for loop. Same content you are going
to write once again, three lines of information, right?
Let me open it and see the result.
So that is the use of your 'append' mode.
That is the use of your 'append' mode. That's it.
So guys, this is the way how you can create a new file or
how you can append new content to your existing file.
Right. Fine.
Now, what I want to do is, I have some file, right. Already
we know we have file called 'with_loop.txt'.
Now, I want to read the content using your Python.
I want to read the content using your Python.
Nothing is there. First whether you want to read or write your content
with your Python, first of all you have to open your file,
but while opening your file, right, you have to write your required
mode here, whether you want to write or read. Now my purpose is,
I want to see the data,
I want to read the data, then open your file in 'read' mode.
That's it. So whether you're going to write your content to your file
or whether you are going to read content from your file you
have to do one thing open and close.
Don't forget about this.
If you don't close, then sometimes your file may corrupt.
Right. Fine.
Now, I opened in 'read' mode.
I closed it, right.
Let me execute it.
No error. No output. Whenever if you are reading means, you should
display something then only you can see the content. Now,
how can I display the content? Nothing
is there. Simply 'print(fo.read)'.
'fo.read'. See the content. You're going to get your entire
data at a time using 'fo.read' operation.
You can also store these into one variable called,
let me take 'data', data of your file.
[no audio]
That's it. After closing your file
you have your data in a 'data' variable.
That's it. See I opened, I read the data, and I stored into some
variable called 'data'. Now, even though if you close your file,
already we stored the file content to some variable, that
variable we can use it.
[no audio]
Right. Fine.
So at a time if you want to read your entire content, this
is the procedure.
Now, what I want to do is I want to read one by one line.
One by one line
I want to read. See that. What I am doing.
Anyway, if you want to read always you have to open your
file in 'read' mode.
Then 'fo.readline'. That's it.
Then let me close it and see the content what you are going to get.
Sorry. You read the data, but you need to print that, right,
then only you can see the data.
'print', whatever the data you are reading.
That's it. This is a, "This is using loop- iteration- 1". Now
I want to read second line, then immediately do once again
your 'readline'.
That's it.
But reading line-by-line, I do not know how many lines you
have in your file.
Right. So this is not a good practice to read your file.
But you have an option. Sometimes, very rare cases you will
use this one also. But remember that usage as well. Now, I
want to read your entire content at a time.
So previously whatever the data you are reading with the
help of 'fo.read', right, see that, let me enable this once.
[no audio]
Let me 'print(data)'.
And what is the data you are having in that? You know, you
have a data, your data, right?
See the output.
What is the type of your data?
That is a string.
[no audio]
Right, your string. You have number of lines in your file.
I want to take line-by-line separately, but I want to read
at a time all the lines.
I want to read all the lines at a time, but I want to take
line-by-line separately, not like a string.
[no audio]
See what I am doing.
What I am doing here is, you have one more option just to read
your entire content.
[no audio]
into a list. First of all, let me open my file,
[no audio]
then let me take 'data=', your 'fo.readlines'.
It's very, very important.
It is very useful in your real time.
Now, let me print your data, and see the output what you are
going to get. You're getting as a list.
You're getting as a list. If it is a list, you know, you can
take for loop, and you can print line by line. Suppose
I want to read, simply I'm giving. If I want to read first three lines,
What I will do is, see that.
[no audio]
'for each in range(3)'.
First let me print
what is the 'each' value you are getting.
There is no relation as of now between your for loop and
your data. Simply
I am taking some 'range(3)', it is going to print 0, 1, 2.
Right. Now this data is a list, right. In list
if you want to read first three values, then index values
are from 0 to 2, right, 0, 1, 2.
So if we take 'range(3)', you are getting your required
index values for your list to get your top three lines.
Now, what I am doing is I am printing your 'data', then index value.
[no audio]
Guys, for first iteration your 'each' is equal to 0. That means
you are printing simply 'data[0]'. Then second iteration
you are getting index value as 1, third iteration
you are getting index value as 2.
That's it. This is the data you're going to print using your for loop.
That means, you're getting top three lines.
[no audio]
Run it.
[no audio]
Right. So guys, now what I want to do is I want to get only last
line. I want to get only last line, how can I get it? You know,
[no audio]
'data[]',
last line, always. Already 'data' is a list, right.
In list, last value index in terms of negatives,
right, -1. Then I can print this one.
Let me comment your for loop.
[no audio]
That's it.
[no audio]
Right. So guys,
this is the usage with 'readlines'.
We have so many real time cases, right. This is just introduction.
We will see some simple real time example in next video.
Okay. So guys finally you have to remember that, right.
See 'write' mode, yeah.
While working with your files you have three options.
First option is you're going to create a new file. First time
you are going to create. Then open in 'w' mode.
So while opening your file in this mode, if already file is
there then what it will do? Your Python is going to delete
that file and is going to create a new file. So that, what
will happen? Old file information will be deleted. So be careful
whenever if you are opening your file in 'w' mode.
I want to keep old content as it is and I want to add new
content, I want to append new content to your file,
then you have to open that in 'append' mode. Now,
I want to read all the content which is there in my file,
then open in 'read' mode.
So whether you are going to write, append, or read, right, procedure
is same. See, 'write',
'fo', file object, open your file, and get into your hand your
cursor. Do your logic here. Finally close your file. That's it.
[no audio]
Right. Fine.
Okay guys, thank you for watching this video.
[no audio]