Standard Error Python | Stdin / Stdout / Stderr (beginner – Intermediate) Anthony Explains #050


Subscribe Here





Stdin / Stdout / Stderr (beginner - Intermediate) Anthony Explains #050


Hello, and welcome to another episode in this episode, we’re going to be talking about the standard streams standard in standard out and standard air. And how you might use them and what they’re for and, yeah, that sort of stuff, so let’s let’s jump into it, okay, so we’re gonna be talking about console applications for these three streams for windowed applications, they don’t apply as much because the 3 streams usually aren’t connected to anything, so we’re mostly talking about concept pages. There are some cases where the streams do get connected for console applications, for instance, like if you launch. I think Update Manager is one example. Oops, up to a manager. If you launch this from the console here, sometimes times, it’ll print stuff here. I guess not this time anyway. But most the time the the standard streams are really only for console applications and the first and probably the easiest – we’re not gonna actually run updates. The easiest to explain here is standard in and standard in is the input stream. It allows you to read from the keyboard. I guess I’ll just do a quick example set. We can do that with Python. Write our text editor in Python. The three streams are accessible through the system. There’s Sis Dot standard in assisted and route and sustad standard error and to read from then you’ll use sista standard and read or read line or whatever it acts like a file and I guess standard out and standard here also act like files and in Unix, they are actually files. You’ll also have these three streams on Windows. However, the examples I’m going to show, probably won’t work unless you’re working with, like git bash or another, A shell that implements bash like syntax, so we’ll start with this, and we’ll, actually, we’ll probably print this just so we can see got this thing. Yes, cool, so if we run this script now. Python 3 you’ll see that it’s kind of hanging here. It’s waiting for me to type input into the program, and if I type HelloWorld and press Enter, that also does nothing because I asked for the entire contents of the file. Please look at it over here, you’ll see. I did, sis that standard and ugly! This is going to read the entire file at once. Now if I press control D. This will signal end of file. So I press control D right here and then. This is the print that we got out of here, so that’s standard in. You can also redirect things into standard in, so if we were to take this file here and redirect in well, we we can do something silly and redirect in a file, which is what single arrow does, and so that’ll that’ll redirect in T Dot Pi, so it’ll put the contents of T Dot Pi as standard in and you’ll see here that we should probably just bang R so that we get the representation here, so you can see that this is the contents of T Dot Pi, and that was what we got. When we did insist on standard N Dot read, you can also pipe into it using a pipe character and this. This takes the standard output of this process, which we haven’t talked about standard out yet, but we’ll talk about it soon and passes that as the inputs to this. You can see that this printed high plus a newline. And that was the input to this next program. Cool, so I think that covers standard in. Let’s talk about standard out next and so turnaround is kind of the default place to produce outputs. Usually you’ll use this. If your program like, make some useful output like the echo program, for instance, it takes all of its arguments and prints them to standard output and in Python. If you just do print by default, prints goes to standard out, you can also use cyst on standard out dot, right, This also goes to standard out usually it’s not a good idea to mix print and cyst out standard out. Dot right, do too. I think they might have fixed this. There’s some like odd buffering that can happen here where you might get things in an order You don’t expect or yeah, basically we’re. Ordering is the main problem there, so let’s gonna comment out the standard and read here, just so this is a little bit easier to see, and you’ll see that these two things print a standard out. Now you can also redirect standard out so this was redirecting standard in you can redirect standard out with an arrow going the other direction. So if we want to redirect that to a file, we can do an arrow to. F and you can see now that this output goes to F. And if you want to redirect this to something that, you know, you don’t see. You can write it to Dev. No windows. This will just be n. UL, which is a special device like thing that magically exists everywhere because windows. You know, Windows is similar to a Linux like distribution, but does does some things a little bit differently. So let’s stand up and yeah, so let’s talk about standard error next. It’s also in the SIS model and you can also write to it using print, but you use the file keyword argument again like standard in standard Downstair Standard Error Act like files, this goes to standard error and you can use file equals cyst on standard air or again. You can do cyst on standard error, right, This also goes to stare and you’ll see when it when I run this By default, Both standard out and standard error will be displayed on my console. I do this here. You’ll see that we get the standard out and we get the standard error. Now think due to the way. I think this is line buffered here, So sometimes these are actually out of order due to the way streams work, But I think almost all the time you’re gonna see these in order And generally what standard error gets used for is well as it kind of, says error messages or other diagnostic information. Usually you want your program’s, Primary outputs, go and standard out and any sort of like logging or error messaging or, you know, problem, statements or stuff that’s unrelated to the data of your output can go into a standard error, like, what an example that that I’ve seen before is like a tool which will format code, it might print the formatted code to standard out, but it might print a message that’s like formatted food at Pi to standard error that way if another program, which means the formatted output, it can just read standard out and the diagnostic messages can go to standard error. And you’ll see if I pipe, You know, if I pipe again. The standard out here. The program is still going to display the standard error. Even though we put the standard out into a file, you can actually pipe both of these at the same time with arrow ampersand. And so you can see now if we look at. F it’s the exact contents that we saw on the on maternal here. Wow, but interestingly, here’s here’s what I was talking about with ordering. Interestingly, here, we got standard error before standard out, even though standard error was printed last and this has to do with file, buffering and stuff. I think if I put in a sista, stand out dot flush here. I think that would make it in order. Yeah, so this this synchronized this dreams and made sure that standard out got flushed before standard error tends to be flushed more readily. Just because you want to see your debugging messages as soon as possible. I think that’s that’s mostly it. For the three standard streams. Hopefully this was useful. And if you guys have additional things, you want me to explain, you know? Leave a comment below or contact me on the various platforms. But thank you for watching, and I’ll see you guys in the next one.

0.3.0 | Wor Build 0.3.0 Installation Guide

Transcript: [MUSIC] Okay, so in this video? I want to take a look at the new windows on Raspberry Pi build 0.3.0 and this is the latest version. It's just been released today and this version you have to build by yourself. You have to get your own whim, and then you...

read more