File Inputs & Outputs

12 minutes
Share the link to this page
Copied
  Completed
For instance, a keyboard or computer mouse is an input device for a computer, while monitors and printers are output devices. Devices for communication between computers, such as modems and network cards, typically perform both input and output operations.

Transcript

Hello, in this video we are going to look at input and output for files. And as input and output in general. So, started, we've already done output, print, default output, we are outputting to the screen so far put in Hello, world. And you might notice that in this video, I'm not running it on the online ID. That's simply because when we get to the file input output, so depending on the online ID, you can't really do is good for test input code, but for some stuff like file input output, it's not the best. So that's why I'm doing this locally.

Obviously, I covered how to set up Python locally on different platforms. So feel free to check out the videos in the intro section. Once you're done with that if you didn't already do it and come back here and we can continue with file input output. Okay, so print hello world, if we will To run this method will save it run module HelloWorld. So you know simple stuff we know all about, you know, printing. Now, we are in master approved, we don't need a refresher course or anything like that.

So let's have a look at the input method, which you know, how often you can get allows us to get some something from the user. And it's just raw input. So I do this, create a variable called var one and inside here, I'm going to assign the content of what we get from the user. So I'm going to do input. Now, we're just gonna do one if I were to run this so enter thanks If I say 78 insides empty, we've been able to get user input. So that's really for user input.

Obviously, we'll go a little further and have a look at you know, opening files, closing files, that sort of stuff. Because at the end of the day, without, you know, being able to access external files, we're going to be, you know, pretty heavily limited. So, I'm going to show you my project directory is just a Python file. So what you can do is initiate a command called Open and the open command will open a file. So we're going to assign the content of the file to a variable so I'm gonna say, var two equals open is the command. To specify the file go to File dot txt, put a comma and from here you can specify the Essentially the mode so you can have off which open the faults and reading only.

And you can open it for reading and writing, you can open it for, you know, in binary format, for example, and it overwrites the file, the file, if it exists, does not exist, it creates a new file. So let's just have a look at it for you know, this reading for now. Our classifier, so, that is it for open. So if we save that, see what we get when you open this down like so. variable, so we can see everything. Now, if we go back, save, re render.

This is a matter so it says no such file or directory and you know, it's right. There is no fun Hear with that name. But if we were to open it in a nother type of way, let's say, if we open in a binary format, which is web, I'll provide a link, we can find all of the different formats. And now forward to run it. I think we've created the file if we didn't exist. And now I'm actually going to comment this input line, because it's just slowing down thinking still be there.

So when you have a look at the source code, you can check it out. So this vaccine contains all of the content of our balls of the print, but to therefore save it and rerun this. That's what we get when it's opened in to Vol. Two. So we can we can get the name of the file, we can go check it out. is closed or now we can check the different mode, for example, and we're going to have a look at reading and writing as well.

So let me just show you how to get the name also like volatile dot name. And the other methods you can check out as well from the link in the description. And there you go, that's the name of the file. The other thing I want to mention is once you really know, done with a file, you should close this or the close method of close the close close method of a file is basically flushing any, you know information that's unwritten and close it the file object, and at which point you can't write any more. So that command would be upgraded down to a question to the last thing, where to go, you know the name of our object, dot close, and that's it. So it's closed for business.

You can't do anything. After that, obviously, you know, no point running because the result will still be the same. So now let's have a look at writing something to the file. So let's write something to the file. So you do var to dot white. And now we can put something in here to read my orders.

This is Hello, my name is Bob. So please save that. And if, as you can see, the contents of this is empty, it says zero bytes. So it will save it. rerun this. Oh, yeah, sorry, my bad, I'll put web what we want to do is put, you know, W four, you know, it's gonna be writable and the writable.

Again, if it doesn't exist, it will create it but it does. exist. So we can just say that the different mode will depend on what sort of content and for parental they go it's a day to day with Hello, my name is Bob, how do we actually read the contents? So reading is actually pretty darn simple. And nothing you can guess the way you do it you do. So we're going to create a string one called door to read.

And what you do is specify a parameter and this is the number of bytes to be read from the open file. And he just started reading from the store. So if I put in two bytes, now if I want to print it as a string one, run the module. Okay, see where we go wrong. What is happening so something has gone wrong so don't read into screw on not readable Oh, he told me why it was so we need to change the mode so it is readable as well. So obviously we want to be able to read the contents plus I will comment out this line for now we already know the content in there already mode can you start making stuff confusing?

Seems like it's out written with the lemons, put something in, favors Hello, rerun this. There we go. It's got the first two boys up, I don't know why the content got deleted, there's some reason for us to do with the mode that messed things up to 10, for example, run this, that gets the first 10 bytes. And all of the content contained within the first 10 bytes, just make sure you close it afterwards as well. So you can also check for essentially file positions, you can check like the current position of a fall like based on its path. And that method is called tau just to double check that out yourself.

There's a method for renaming a file. So if I go here, and To rename a file, we want to actually do an import. So import OS. And what we can do is we can rename the need to be when we've opened the file, we don't need the file open for this. So let's do OS dot rename. To specify the name the name of the file we rename in.

So file dot txt. We are renaming it to name dot txt. And as you save that, rerun the module. As you can see, it's renamed it to new name t x t we can also remove a file at the next task I want to do that yourself and that's just removed that was actually pretty removed And it's just the file name itself. So, file name as a file location, I guess to the folder, folder location, and file name. This is not a good place what you want and just be careful that you don't delete some file.

That's important to you. Obviously, always be careful that you can also make a directory for example, so, you can do this can do ls.mk dir, this will make a directory a new folder, save that from the module. This does not exist. they renamed it. I am going to actually comment this acquisition is going to cause error problems just because you know do applet program later for education purposes, and you know, literally just created the folder there. You can also change directories as well.

You can remove directories with the RM dir command. There's like so many methods that you can use. Once you have got to grips with what we covered in this video. Honestly, you're all good to go. Knock said I'll provide a link in the description so you can see the more advanced functionality as well. Feel free to check that out.

Thanks for watching, and I look forward to seeing you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.