EX 26 trying to print file content

Hi there, I’m going through exercise 26 from the Learn Python the hard way and I want to print the content from my text file but it’s not printing.

from sys import argv

script, filename = argv

print(“opening the file…”)
txt = open(filename)
text_to_print = txt.read()

print(f"The content of the file is:\n{text_to_print}")

The code looks fine. Are you sure there’s something in the file you’re trying to print? Maybe you had something typed in an editor buffer but not saved it yet?

1 Like

Yes, common mistake on this is not saving the files in your editor. It’ll look like you typed the text in but until you actually save it they aren’t on the disk. You can check this with the command:

cat test_ex20.txt

If that prints out nothing then it’s not been saved.

1 Like

hi, thanks for the comments. I will check if I saved the file.

A free service run by Zed A. Shaw for learncodethehardway.org.