Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Adding Content to a File

00:00 Okay. So I would say let’s add some content to it. So since it’s a Python file, let’s start with some print() function. Yep. print("Hello, Terminal!"). Or is there anything else you wanted to show?

00:13 Just one more kind of convenience: I can type Control + L, and that just clears the screen for me. I could also spell it out like clear, like this, but it’s much quicker to just do Control + L. Then it clears everything for me.

00:26 Perfect. Yes, let’s add some Python content to the file. There’s a couple of ways of doing this and I think I want to show you two different ones that are both useful in their own ways.

00:37 So one is inside of the terminal, we kind of have a print command where you can just print out stuff. So I could say something like Hi Philipp! like this.

00:49 And you can see that it just prints this out.

00:52 So the command I’m using here is called echo. So this just takes whatever I put here and just pops it out on screen again. So I could now start writing Python code here, and I’ll throw this inside of a couple of quotes and say "Hello, Terminal".

01:10 Is that what we’re doing? Exactly. Like this. So if I do this now, I just print this to the terminal, which is still not exactly what you asked me to do.

01:20 We want to put this into a file. Exactly, because at this point in the terminal, it’s basically only a string that you are echoing there. So we know it’s Python code, but for the terminal, it’s just any string. Yeah.

01:34 And just to point out here that this hello_terminal file is still empty, you can see the content has 0 size. Okay. Yeah.

01:42 So what you can do inside of this terminal is that you can redirect the input and output—so where things are read from and where things are stored to and from files.

01:53 And in this case, what we really want to do is to take this output that we have right there and redirect it to a file. And the way we do this is to use this > (greater-than) operator right there,

02:06 and then I can spell out the filename that I want to send this to. So now if I run this, we can see that nothing was printed on the screen here, but if I do my ls -l (long listing), we can see that this now has some content right there. Okay.

02:21 And if there would’ve been any content in the hello_terminal file, it would’ve been overwritten? Yes. Okay.

02:30 And this is kind of scary. This is a very powerful little operator that kind of just sits there bause it will overwrite whatever’s in a file, and if you kind of start the command and realize, okay, this is running too long, and you break it or something, if that command is redirecting to a file, it’ll already have deleted the file for you, or deleted the contents of that file.

02:51 So be careful when you’re typing like this. Yeah, maybe that’s a good moment to emphasize this part. In the terminal, you were saying the terminal is silent when things are just working, which also means there is not a confirmation message or something like this, like do you really want to write this content into the file or do you really want to delete this file or something like that, which you might be used to on a graphical user interface, but with the terminal, you have all the power most of the time.

03:21 Most of the time. There’s a few things it stops you from doing, but it’s very easy to also delete stuff. Yeah.

Become a Member to join the conversation.