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 the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

Updating File Contents

00:00 And yeah, as you just showed, now our hello_terminal.py file is hello_world, but it still has the old content that you showed with the cat command.

00:10 It still has print("Hello, Terminal!"). So, for this file, I want the print() function saying "Hello, World!".

00:19 Okay. So then I will do the same thing I did before again. I will say echo 'print("Hello, World!")'

00:31 and close the single quotes and then

00:36 redirect the output into the file. And this is going to overwrite whatever is in there. So after I execute this, now

00:48 the file contains just one line that says print("Hello, World!"). So, if you use this character, the > (greater than) symbol, then it’s going to overwrite whatever is in the file and put in there—replace it, I guess. Okay. So, that’s important to know.

01:03 With this one > sign, you’re basically overwriting whatever is in that file. There’s a—if you wanted to add something, you could use a double of this symbol (>>), and then it’s going to append it at the bottom of the file. Go for it.

01:18 So, if I execute this, and then print, then I have two of those lines in there. Okay, that’s nice. Let’s see, when you run this file, if it actually outputs Hello, World! two times.

01:32 I’m going to replace it again, though. I will only have one Hello, World!. Is that okay? Okay, yeah. But then let’s emphasize, so this one, you’re using one > sign there again, and you are replacing the whole content of the file now with what you’re echoing before that, which, again, is just a print("Hello, World!"). Okay,

01:51 cool. And then let’s run it. Let’s run this file.

01:57 Okay. And now I want you also to run the hello_terminal file in the hello/ directory. Okay. hello/hello_terminal.py. There it is. Perfect. Okay, so you don’t have to be in the same directory as the the file that you are running in this case with the python command, but you can also add a path to it, and if the path is correct and there is a file called hello_terminal, then it will execute this file. Yep. Okay, so this was everything that I want you to do. So thank you very much, Martin.

02:32 One thing that I wanted to talk about to wrap this up is the terminal usually don’t asks you if you really want to perform this action. So, sometimes if you delete a file in Finder, you get this little pop-up like, Do you really want to delete this file? or you have to press Cancel if you don’t want to. But in terminals, basically you put in a command, you overwrite the whole file with this new print() function, and with the press of Enter, it’s just replacing this file.

03:01 And there is no easy way to undo such an action. So that’s something that you have to keep in mind with the terminal, that you’re very cautious about the commands that you are using.

03:13 And if you are unsure, look it up again or try it out with some dummy file that you just create for this purpose before you use important files that you’re having on your system.

03:24 Cool.

Become a Member to join the conversation.