Let's now talk about how we can read from and write to text files.
So say, for example,
we wanted to create a program that would accept an input from the user.
Let's say the user has been asked to provide their username
and then we want to store that username on a separate file. How can we do this? Well,
first things first is let us assign a variable
that will accept the input from the user.
So I'm gonna call my variable username equals and now the input function
and now enter your user name, put it straight forward.
Now, remember from the previous lesson are
with open
syntax, I'm gonna say with
and now open
and of course, remember open will, will accept two parameters.
First of all the file you want to work with and then the operator,
I'm gonna create a file called users dot txt.
And then comma
we're going to add the W
function
to, right? And then I'm gonna say
file,
OK?
And now what do we want to do?
We want to write the username to the file.
So what I'll do right now is very straightforward file dot
write.
And now in brackets
user name,
OK?
And then just for good measure, we can print out something
after the user has provided the username. So let's say print
and
I will say a username
has been added
to the file
just as an example. OK? So
let's go ahead right now. Run the program,
enter your username. I'm going to say Alex
press enter and now it says username has been added to the file and I can see users do
text file has been created. And if I open it,
you can see it's right there, Alex
so far. So good.
However,
if I go back and I run the program again
and this time I used a different username, I said Alice, I press enter.
If I go to my user or text file,
you can see right now that Alice has overwritten Alex.
And that's because whenever you're accepting inputs from your users,
your storename in a text file,
when you use the right operator,
it will simply overwrite whatever it is that existed in that file previously.
So what if we don't want this?
What if we just want to keep on adding files
or usernames to our file without overwriting the previous ones?
How do we do this?
First of all, let me clear my history. Remove all users dot Text file.
OK.
So let's pretend like we're starting from scratch
instead of using the W
function, I'm going to use a
which represents a pen.
So with append,
we can keep asking the user to provide a new username.
And when once they keep on adding new usernames,
they will be stored in our file and the previous usernames will not be overwritten.
So
I'm going to run the program again.
Provide my username, Alex.
OK?
And now you can see we have our users dot TXT file,
Alex just like we did with the right operator, right?
But now if I go back,
I run the program again.
And this time I say Alice,
what now happens? I go to my users dot text
file and there you go. We have Alex, we have Alice.
So you can see Alex was not overwritten.
And by the way,
if you wanted to store your usernames in separate lines,
all we need to do right here is where we write in the file, username.
We can just say plus,
OK? And now
check this out,
you wanna say
forward slash,
I'm sorry, backward slash. And then N
so this right here, this is what we use to
create our lines. OK? So
whatever you're adding will be added onto a new line.
So
what I'm gonna do here is let us remove
the users dot text
file again.
Let me run the program
one more time,
Alex press enter,
let's run the program again.
And this time I'm gonna say Alice
press enter
and now if I open up my users do text file
now you can see Alex and Alice are on separate lines.
So this right here is the difference between
your light operator and your append operator.
But what if we wanted to read
the data, the usernames from the file? How can we do it as well?
I'm just gonna come down in here, say with open again
brackets.
And now I'm gonna say users dot TXT
and then what are we gonna do?
Codes are?
OK? And then as file
call on
and now
we want to create a for loop. So I'm gonna say for
name,
OK? For name in file.
So for the usernames, for the names in the file, what do we want to do?
We want to print, I'm gonna say print
and then we can say, OK,
uh username
colon and now
add a
comma
and now name
dot
strip,
this is a new function and it's going to
have its own
empty brackets
and
there it is. So now if I run the program
Alex
and there you go,
using them has been edited to file.
And now because of our function over here
where we read the file,
it's gonna say username,
Alex, username,
Alice, and then username Alex again because the user dot text file has Alex
Alice,
Alex.
So this is how we can read
the contents of our file.
And by the way, the dot strip right here,
this is a method that's very often used whenever we are handling files.
Now we use it to remove any sort of like leading or trailing white space characters
from a string.
And this may include things like your spaces tabs or even new line characters.
So it's just a very efficient way of ensuring that whatever text we are
outputting or printing out
it is in the most efficient sort of manner without any unnecessary space.
That's why dot Strip is very often used
whenever we're handling text files.
Now it is completely optional. You don't have to use the strip method. In fact,
if I removed
the strip method
and just close my bracket, normally,
if I run the program,
it will work the exact same way. So let me provide a new name. Let's let's say Mandy
as an example. And there you go, we have username Alice, user name, Alex name Mandy
and there it is, it's still gonna work.
But you can see right now that we do have the extra space, the lines between
uh username, Alice, username, Alex,
username Mandy because we are using the new line character, the new line string here
to add each input on a separate line.
So that's pretty much it for reading and writing to text files.
Thank you for watching. I will see you in the next class.