2.6 File I/O - Opening, Reading, and Closing

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *

Already have an account? Sign In »

2 hours 30 minutes
Video Transcription
Hello and welcome back to Intermediate Python here on Cyber Eri on Demand Eyes always in your instructor Joe Perry And you were watching the second half of lesson to file Io in this video, we're gonna do two things. One we're gonna take the simple lessons we learned in the last video in which we figured out how to read and write from files.
We're gonna put that into an actual python script taken, take user data and store it in a file,
and then we're going to spend a little bit of time talking about the idea of non text files. So first, we're gonna do our quick project here to save him.
File store, not pine.
We're gonna give the shebang line,
define our main and put a return in there and then invoke main.
And then, as we often do, we're going to write our thesis for this program, appear at the top as a comment,
takes an arbitrary amount of input
and stores it
in a file.
So we learned in our last lesson in less than one. We learned how to take input. So first, what we're gonna do is we're safe file
name equals open people's
I would just say file name.
Then we're going to do
equals open
file name
for writing.
Now, this is not a very robust way to write this program. One of the things that you're going to be working on in one of the code exercises is howto actually interpret and make sure that files don't exist before you blow them away with writing. That's gonna be one of the challenges of one of the lab. So we're not going to show you how to do it here, obviously. But that solution code does exist.
S so what we're gonna do here is we're just going to assume that whatever file name the user gave us is good.
We're gonna blow that file away. If it exists and hope it's not important, we're going to say, OK, fantastic. Now we're going to f dot right?
And we're going to shortcut this a little bit because instead of taking the input and storing it in a variable, we can just call input inside of F dot Right, Since all of the input in python starts a string until we interpret it differently, we'll just say input.
to store.
And whenever that's finally don't
not enough parentheses and then whatever that's done, we'll just f dot clothes
and we will exit the program.
So this again is what we did in our last video. Just kind of written as a script we can test to see if it works with a quick ahmad
file name. We'll call it
put this dozens exists, not text.
And the day the store is just gonna be some junk data.
Now, as with most input, that data is going to be ended whenever you give it a new line.
And you can see here that it is finished executing that script and we contest to see if it executed correctly simply by looking for
I hope this doesn't exist dot text. There we go.
Sorry. My VM bugged out on me a little bit there,
and we can open that up and see sure enough, it contains that junk data. We provide it
so very straightforward. Very simple, hopefully pretty easy for you. That is how you can write a program that takes in stores input from the user. Now, that's something that you might be able to use in conjunction with our previous lesson where we talked about token ization. So instead of just taking a file named some arbitrary or data, you might take large amounts of information
and say that the first token in this string is the name of the file to store it. And
the second token contains how much data or something like that. And you can kind of combine these two concepts as you'll see in one of our labs
to create more robust storage processes,
and that, interestingly enough, is actually the fundamental basis of a database. That's how databases air fundamentally constructed. So when you start looking at things like sequel databases or General relational databases, that's actually what's happening is that their token ized. And they interpret that data and they store it into your program, where they store it into your database. Ah, lot of
sequel databases have Python front End's for easy use
and a lot of those Python Front End's implement organization almost exactly like how you saw it in the previous lesson. So these are not contrived examples. These are actual examples of how you can use python code
so very quickly. The last thing that we want to look at here in lesson to is how we can open and use files that aren't written as text. And in order to do that, I've actually downloaded another file and see, here is the hero dot jpeg, which is not a text file. It is a picture. It's an image file. It's a dot j p G.
So what we're gonna do is we're gonna open up our python three interpreter
there are screen up a little bit. Now, this is gonna work very similarly to how opening files did previously. We're going to do F equals open
file name as a string,
and then we're going to give it the mode. But instead of just being, are we actually going to use our B, which is read binary? Which is to say that this is not a text file. This file is some sort of non text
junk. It could be really anything. It could be a jape, eh? It could be a portable execute herbal. It could be all sorts of different stuff. So we're gonna open this j peg and we're going to do now is just have a look at what this data. Looks like we're going to do an f dot reed
and just see kind of what this data
looks like without any cleaning up for interpretation.
You can see here that it just print out all of these different hex bites and ends with these three no bites, which is the end of file signature for this for this format.
So this isn't really readable. This isn't useful. Interpret herbal data.
But if we were trying to perform an operation against this J peg like you might want to do in one of the advanced Python labs and our upcoming class, this is the way you would want to open that file so that you could modify the bites individually without messing up the actual or without, you know, trying to read them a strings. Because if you try to read it and I'll show you here
not the but I meant to press.
If you try to read, it is a strength.
It's not going to work out the way you want it to.
f equals open
and again, we're gonna call it here about J. P. G. And we're just going to open it in reading mode. Now you'll see that it opens, find the file is opened and the handle is returned without any other information or the file pointers or turn around without any other information. A little bit of Windows programming snuck in there, but we're trying to f dot reed.
You can see we're gonna get this error. And that's because this Kodak the UTF eight that the
language Kodak they were using doesn't know how to interpret various hexi decimal bites. It doesn't know what those bites are intended to be,
however, there. That isn't necessarily to say that it's always impossible to read anything out of these files when you're trying to read them, you know, trying to find strings or trying to find content and a great example of that what we're gonna do real fast and I'm going to make a super trivial dot C file
we're going to do
this is going to be the sea version of that hello world that we wrote in our previous course. We're just going to print up,
and it's okay of nothing that I'm writing here seems to make any sense. This is not a C class. You don't really need to worry about any of that will run things every w Q. Where he's going to do G c c h w dot c
We're going to attack Oh, h w
And then we're going to run a tch w and see that it prints out hello world with no new life.
Now that we have this execute herbal file, this isn't a J pick. This is a different style of file, but we can still interpret it and use information from the python. And we could see that by opening our interpreter again
And doing f equals open
h w
f equals open
h w.
And again we're gonna open this in read binary
There was going to do f. Reed. We're gonna read the 1st 3 fights,
and you can see here actually start the 1st 4 whites
there were Third Time's the charm. You can see here that sometimes text characters actually do show up This LF is actually the portable execution executed will signature for and a Lennox binary for Olympics per portable executed.
So it's really useful for this. One of the things that you can use and python very commonly is if you're trying to sort or interpret files. You can open them in this read binary mode and still find strings and interpret the data in the headers of those files.
So that's one of the really cool things you can do with Python when you're reading non text files is still fine strings or still find interpret herbal data in the header. And one of our advanced projects is actually going to be interpreting files based only on the headers to determine what their file type is that will be in our course That's coming out in a few weeks. It's going to be one of the harder challenges we have,
but that's just give you an understanding of
what we're doing right here. This file interpretation is a really, really useful utility. They can actually be applied to a bunch of different areas of the programming discipline,
so that's going to be the end of lesson to in this lesson. Again, we talked about creating, reading and writing from files and python. We actually wrote a python script to store data given to us by the user, and we learned how we can read non text files and python and I show you how you can find a header for a piece of header information in a python follow. So that is gonna be the end of
this lesson lesson too. Which is all about file io.
When you come back, we're gonna be talking about less than three. The final lesson of this first module where we're going to learn to interface with our operating system using the OS module. Very excited about that lesson. I hope to see you back here. As always, I have been your instructor, Joe Perry, and you're watching intermediate python on Cyber eri on demand.
Up Next