0

I'm working on assignment 7.1 on the Python for Everybody specialisation from coursera. The assignment is the following:

"Write a program that prompts for a file name, then opens that file and reads through the file, and print the contents of the file in upper case. Use the file words.txt to produce the output below. You can download the sample data at http://www.py4e.com/code3/words.txt"

When writing my code in atom and running it in the Linux terminal, I found that it did not work (bearing in mind I made sure to save the file words.txt that was given to us in the same folder that I'm in when I usually start python). But when I ran this code in the web-based python autograder that is used for this course I found that the code worked fine. Here it is:

# Use words.txt as the file name
fname = input("Enter file name: ")
fhand = open(fname)
inp = fhand.read(fname)
for line in fhand:
    line = line.rstrip()
    new_inp = line.upper()
    print(new_inp)

When running this in the Linux terminal the syntax error I receive is:

Traceback (most recent call last):
  File "chapter7.1.py", line 2, in <module>
    fname = input("Enter file name: ")
  File "<string>", line 1, in <module>
NameError: name 'words' is not defined

Is this not working because I have misplaced the file(words.txt) even though I thought I stored it in the correct folder? OR is this not working because of something else? In the past I have also had minor issues where code that runs in the python autograder online does not run in linux without a traceback. I have a chromebook if that's relevant at all and the software I downloaded to run python in linux is minoconda.

  • 1
    `fhand.read(fname)` -> just remove this statement. with `fname`, it should throw error (byte number is expected, not a string), and even without `fname` it will move pointer to the end of the file, which will screw the following loop – Marat Aug 23 '20 at 15:23
  • @Marat Read the error message again. That's not the problem OP's currently experiencing. – wjandrea Aug 23 '20 at 16:53
  • @wjandrea it might not be enough to fix the problem, but this statement definitely should be removed for the reasons I explained – Marat Aug 23 '20 at 17:01

1 Answers1

0

You're running it with Python 2. Run it with Python 3 instead.

To explain, in Python 2, input() evaluates the string it receives from the user, so when you give it words.txt, it parses words as a variable name, and txt as an attribute, but the variable words doesn't exist, hence the error. In Python 3, input() doesn't evaluate anything. For more details, start with this answer.

Also note that you're receiving a NameError, not a SyntaxError.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • thank you for your answer. But even when I manually evaluate the user's input in my code I receive the same traceback. – Shehzadi Aziz Aug 23 '20 at 17:33
  • @Shehzadi "Manually evaluate"? No, I'm saying that's the *opposite* of what you should do. Did you switch from Python 2 to 3? – wjandrea Aug 23 '20 at 17:43
  • Sorry i’m still a bit confused as to what you mean, Miniconda3 which is what I have installed on my computer, runs python 3 in Linux. – Shehzadi Aziz Aug 23 '20 at 20:33
  • @Shehzadi Try replacing `input` with `raw_input` and see if it works. – wjandrea Aug 23 '20 at 21:27
  • @Shehzadi No need to apologize. I'm saying, in Python 3, `input()` should return **a string**, and there's no way that that error could occur, so I'm sure that you're running your code in Python 2, not Python 3. Maybe you haven't activated the conda environment or something. There is another alternative - that some part of your Python 3 config is overriding `input()` as `eval(input())` - but that seems ridiculous. – wjandrea Aug 23 '20 at 21:34
  • @Shehzadi To check what version and interpreter you're using, put this in a script: `import sys; print(sys.version); print(sys.executable)` – wjandrea Aug 23 '20 at 21:37
  • I can now see that linux is actually running python 2.7.16. So yes when changing input () to raw_input() the code actually does work. Thanks for your help. – Shehzadi Aziz Aug 24 '20 at 11:56