Python 3 - Ex15 Nothing printing, no error message

from sys import argv

script, filename = argv

txt = open(filename)

print(f"Here's your file {filename}:")
print(txt.read())

print("Type the filename again:")
file_again = input("> ")

txt_again = open(file_again)

print(txt_again.read())

The program has been going smoothly until this point. When I type in my command in the terminal, “python ex15.py ex15_sample.txt” nothing is printing or showing up. There is no error message. Its the same as hitting enter. So far I have:

  1. Double checked all my spelling and characters, and made sure I was following the directions exactly.
  2. Double checked that I saved my .py file in the right directory.
  3. Asked a friend.
  4. Googled my problem. I have only found occasions in which the code was written wrong, and I have written the code exactly.
  5. Started over twice.

Any help would be appreciated.

i think…you should do txt.close() after the first read.
As the file is kind of open already and again you trying to read it.

I appreciate the suggestion. I tried it and was unable to get python to react to the file. I believe the program is opening and closing itself without leaving me a trace of the activity.

When I paste your code into my text editor, the print statement print(“Type the filename again:”) doesn’t display the quoted text correctly which indicates a syntax error of some kind. The quotes look like the likely culprit to me as the are the curly variety. I’m not sure how to even do that on my keyboard, so not sure how you got them unless you cut/paste from the book or something. When I fix the quotes, I get the program to run. But, I tried without replacing the quotes and got a SyntaxError: invalid character in identifier which seems like you are not seeing…

I can’t really think of what could replicate this issue, but it is curious if nothing happens when you run the program. Even if the sample text file was empty, the input("> ") should show up.

1 Like

Hi Emil,

Thank you for the reply. I cut and pasted from the book after driving myself nuts not finding any of my own typos. After reading your post I went back and rewrote it verbatim, then added input("> ") to see if I could get my prompt to populate. I pasted into a text editor to make sure my quotes aren’t curly. That the program runs for you blows my mind. I am at the point where I feel uninstalling and reinstalling both powershell and python might fix this bug.

Thanks again!

UPDATE: Realizing that I was running Python 37, I uninstalled and put in Python 36, and I am having that same problem. All the other exercises are still working.

One more idea:
Did you call the program with quotation marks like you wrote above?
"python ex15.py ex15_sample.txt"

If so, try this:
python ex15.py ex15_sample.txt

And even better:
python3.6 ex15.py ex15_sample.txt

Hi Didier,

Thank you for the idea. I did not use quotations when I called the program. I don’t know why, but writing “3.6” gives me an error as it did when I put in 3.7 before I switched. Only “python”, without quotes, brings up the program in powershell.

I also do not know what I did differently, but I deleted my ex15 file and started over AGAIN, and now it appears to be working. I suspect that it has something to do with creating the file again after going back to python 3.6, but I have no idea. None.

Thanks again and have a good day.

I think I found your problem. You forgot to set the import of argv on the beginning of the file.
Add this to the beginning of your file (as the first line):

from sys import argv

If I type the following code on in my Command Line (I’m on Linux not Windows) it works just fine:

python3.6 ex15.py ex15_sample.txt

The whole code

# File: ex15.py

from sys import argv  # this line is very important because you have to import the module argv

script, filename = argv

txt = open(filename)

print(f"Here’s your file {filename}:")
print(txt.read())

print("Type the filename again:")
file_again = input("> ")

txt_again = open(file_again)

print(txt_again.read())

I appreciate the time you put into this reply. I had “from sys import argv” as my first line as far as I could tell. At least I know it from memory after staring at it for two days.

Thanks again.

Just put my code in a testfile on your computer and run it the way I showed you. Does it work for you too?

Another idea. Do you have a file called ex15_sample.txt in the same directory as your Python script? Does it contain some content? If you have no content in that file you will see no output. Nothing in, nothing out :slight_smile:

Try to put something like this in the file ex15_sample.txt:

I'm a testfile.
Nothing more

And you should see that two lines after executing your script.

Hi Didier,

After going back to python 3.6 and creating a new .py file I have been able to get it to run normally. I can read .txt and complete the exercise. Thanks again.

1 Like

Awesome @karstenbob. One hurdle removed :slight_smile:

I had this issue and went back and realized that I forgot to save ex15_sample.txt. I did everything correctly and wrote it out with no copy/paste, but moved on to create ex15.py and didn’t save the first file.

Maybe when @karstenbob re-did the exercise, they saved it that time. Just a thought.

1 Like

Python 3.6 vs. 3.7 shouldn’t have mattered, so most likely something else was going on. Can you run this new working code under Python 3.7 to see if it was? In theory, if it was caused by 3.7 then running this code that works under 3.6 shouldn’t work.