Python Coder | Simple | How to open | read a file

Python Coder | Simple | How to open | read a file


You’re watching the Python Coder. In this
video we look at how to read a file. Do you take out all your money when you
go shopping? No. When you were eating snacks and you and you want to eat some do
you take all of them out? No. In a file everything is a string so you could take
out all the string or you could just take it out by one end. So your choices
read everything or get a piece by piece. Well how do you do that? When you read a
file you put it all in memory you can use a file handle to take out parts of
the file bit by bit. What is a file handle? Well this is an
object that points at the file a bit like this arrow here and it can go down
the file and retrieve bit by bit so when we want to get data we can get for
example line by line. So in a file you see many different lines in this
example represented by the different lines of string but your computer
doesn’t see that your computer sees a continuous string so how does it know
what lines to produce? Well that’s a newline character this n and
that means each line that you see has this newline character at the end so we
have a continuous string but with these newline characters telling the computer
when a newline occurs. Let’s have a look at an example. Here’s a
file with people’s names and their scores now this is a continuous string
we have the name a space a score which is an integer and then the newline
character so that’s all one string to the computer. So remember when we get
data we get each line, each line and the name and the score and the newline
character. We don’t want the newline character so we can use strip. So here
we’ve got the data without the newline character. Everything is a string so if
you want to do anything with the numbers as an integer then you need to change
them to either an integer or to a float. So you’re converting the type. So first
of all each line we’re going to split so we split it into x and y the X being the
name and Y being the score and we want to change the score from being a string
to an integer so here we just use int Y. How about writing to a file? It’s the
same in a sense that we have this file handle and we’ve got for example that
people’s names and their scores we want to write them into a file. Remember we
need to make everything a string so if we’ve got numbers they need to change,
well you need to change their type to a string. So again in our example we’ve got
the name and a scores and we need to change those integers to strings. So what
do we do when we write? We open the file, we write the name, we write the score
which we change to a string and then we’re going to close the file so that
means the new folder we’ve created has the people’s names and their scores
inside. So that makes our new file with the scores in. Let’s look at some code including our
example here we’re going to read everything so we’ve got our file called
test it’s a text document we’re going to open it we don’t have to use the R but
the R stands for read so we’ve got open an our file name and a flag for read or
write for example and then we’re just gonna print what the file handle is
dot read so that reads everything into a file using a file handle. So here we’ve
got the same code but instead of reading everything we’re going to use a for loop
with the line using a file handle and then print each line. In our example we
had to strip each line of the newline character so here we see line equals
line dot strip, then in our example we spent the names and the scores here
we’ve got x and y equals line dot split. We had to change the string scores into
numbers into ints so we’ve got int(y) and finally we put our names and scores
inside a scores dictionary so here’s the full code for that. There is an
alternative method of opening files and that’s using ‘with’. So here we have the
same code except ‘with’ open file as file and then we’ve got our for loop. We can do
the same we’ve read as well using with The advantage of ‘with’ is that you can
close the file so people tend to like that it automatically closes a file
after opening it. Remember if you’re going to use file handling then the file
should be in the same folder as your program. If it’s not then there are ways
that you can access which folder or directory you want where
the file is but for now we’re going to assume that file is in the same folder
as your Python file. What about writing to a file? Similar code but here we’ve
got this handle out and we’ve got open the file and the W standing for write. We have a for loop so here we’re going to go through all the names and scores
in the scores’ dictionary and we’re going to write the name which is a
string the score which we’re going to change to a string so string of Y plus
the newline character remember we can use the plus symbol to concatenate strings
and then in this example we’re going to close the file so out.close. If you
prefer use with here’s the code very similar we don’t need to close it after
because we’re using the with method. One thing to notice we’ve got my file dot
txt if it already exists we’re going to write over it so be careful if
you’re going to use a file name then you’re going to delete that file if it
already exists in that folder so be careful there .We’re going to finish with
some terms file handle an object that can access a file slash n stands for
newline and that’s used to split a continuous string into lines of text
such as in a file. Strip is the function we use to remove unwanted characters
like the newline character and also unwanted spaces, read we’re going to put
all the text file into memory, finally write is a function when we want to put
text into a file. OK I hope you enjoyed it if you did please click the like and
if you haven’t already please subscribe thank you for watching and see you for
the next video.

One thought to “Python Coder | Simple | How to open | read a file”

Leave a Reply

Your email address will not be published. Required fields are marked *