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 *
or

Already have an account? Sign In »

Time
2 hours 30 minutes
Difficulty
Intermediate
CEU/CPE
2
Video Transcription
00:00
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.
00:18
We're gonna put that into an actual python script taken, take user data and store it in a file,
00:22
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.
00:29
File store, not pine.
00:33
We're gonna give the shebang line,
00:41
define our main and put a return in there and then invoke main.
00:46
And then, as we often do, we're going to write our thesis for this program, appear at the top as a comment,
00:52
takes an arbitrary amount of input
00:58
and stores it
01:00
in a file.
01:06
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
01:11
name equals open people's
01:15
input.
01:18
I would just say file name.
01:23
Then we're going to do
01:23
equals open
01:26
file name
01:30
for writing.
01:30
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.
01:52
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.
01:56
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?
02:02
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.
02:16
Insert
02:20
data
02:21
to store.
02:24
And whenever that's finally don't
02:27
not enough parentheses and then whatever that's done, we'll just f dot clothes
02:32
and we will exit the program.
02:35
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
02:46
file name. We'll call it
02:51
put this dozens exists, not text.
02:57
And the day the store is just gonna be some junk data.
03:00
Now, as with most input, that data is going to be ended whenever you give it a new line.
03:07
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
03:19
I hope this doesn't exist dot text. There we go.
03:22
Sorry. My VM bugged out on me a little bit there,
03:24
and we can open that up and see sure enough, it contains that junk data. We provide it
03:30
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
03:49
and say that the first token in this string is the name of the file to store it. And
03:53
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
04:00
to create more robust storage processes,
04:02
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
04:21
sequel databases have Python front End's for easy use
04:25
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
04:36
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.
04:55
So what we're gonna do is we're gonna open up our python three interpreter
04:59
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
05:09
file name as a string,
05:11
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
05:24
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
05:36
and just see kind of what this data
05:40
looks like without any cleaning up for interpretation.
05:44
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.
05:51
So this isn't really readable. This isn't useful. Interpret herbal data.
05:56
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
06:15
not the but I meant to press.
06:16
If you try to read, it is a strength.
06:18
It's not going to work out the way you want it to.
06:23
Three
06:24
f equals open
06:26
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.
06:45
You can see we're gonna get this error. And that's because this Kodak the UTF eight that the
06:49
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,
06:57
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
07:16
we're going to do
07:30
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,
07:43
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
07:54
We're going to attack Oh, h w
07:56
And then we're going to run a tch w and see that it prints out hello world with no new life.
08:01
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
08:11
And doing f equals open
08:15
h w
08:18
f equals open
08:20
h w.
08:22
And again we're gonna open this in read binary
08:26
There was going to do f. Reed. We're gonna read the 1st 3 fights,
08:30
and you can see here actually start the 1st 4 whites
08:39
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.
08:54
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.
09:07
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,
09:26
but that's just give you an understanding of
09:30
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,
09:39
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
09:58
this lesson lesson too. Which is all about file io.
10:01
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