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.