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.

Showing and Editing the Content of a File

00:00 Okay, now I want you to show me that it’s actually written in the file. So I trusted you that it’s there. You also showed me, like, 26, which I think is kilobytes or bytes. Bytes. Yep.

00:15 So there’s a very versatile command called cat that here I’m using it to show the content of a file. And again, this is kind of, the name may not make sense, just like with touch, and it’s because we’re using this one slightly different from how it was originally planned.

00:34 So the cat command, it’s really short for concatenation, and what its original role was to just take several files and concatenate them together into a bigger file.

00:43 But if you just take one file and concatenate it, it’s just the same file. So by catting one file, we’re just printing that one file to the terminal per default.

00:53 So there we can see this is a really nice way of showing the contents of a text file. Cool.

01:00 I also want to show one more thing that cat can do, and this kind of comes back to what we already did here with echo and something that I use also quite a lot, because here I was kind of echoing this print(), which could be nice if I want to go back and redo it.

01:14 But an even quicker way to just throw something into a file is to use cat to do it. So if I just say cat but no file, and then I redirect whatever I’m doing into, well let’s do hello_cat then just for fun, you can see that now it just, I didn’t get my prompt back. Instead it’s just sitting here.

01:33 What it’s doing now is that I have this standard input so I can input stuff just by typing them here. So now I can just say

01:43 print("Hello, Cat!"), for instance, like this and Enter, and now I could continue writing more lines, or when I’m done, I’ll do a Control + D, which is how you end.

01:55 And what I’ve done now is that I’ve created this new hello_cat file as well. So this is maybe an even quicker way than using echo to create stuff directly on the terminal. That is true.

02:08 Especially when you want to work with multiple lines, it might be easier to have this input sitting there waiting for you instead of making sure that everything is inside of the quotes, and you have to keep track of the lines.

02:22 Yeah, and this thing also works fairly well with copy/paste, so I could take something and just paste it in to a file like this.

Become a Member to join the conversation.