-2

Working on a Head First Python book, 2010, I've encountered an exercise where I had to print a list into a specific file, and another list into another one. Did all the code, everything works, except for the print module which says the name of the file is not defined, which is pretty weird since the solution of the exercise it's the exact same code of mine.

import os

man = []
other = []

try: 

    data = open('ketchup.txt')

    for each_line in data:
        try:
            (role, line_spoken) = each_line.split(":", 1)
            line_spoken = line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()
except IOError:
    print("The data file is missing!")
print(man)
print(other)

try:
    out = open("man_speech.txt", "w")
    out = open("other_speech.txt", "w")
    print(man, file=man_speech)          #HERE COMES THE ERROR
    print(other, file=other_speech)

    man_speech.close()
    other_speech.close()
except IOError:
    print("File error")

Here is the error from the IDLE:

Traceback (most recent call last): File "C:\Users\Monok\Desktop\HeadFirstPython\chapter3\sketch2.py", line 34, in print(man, file=man_speech) NameError: name 'man_speech' is not defined

Am I doing something wrong about the syntax, or maybe I didn't get how the print module works? The book doesn't give me any clue about it. I've also checked for lot of questions here and in some other forums, but nothing seems wrong with my code, and I'm actually tilted.

Monok
  • 39
  • 1
  • 2
  • 10
  • `out = open("man_speech.txt", "w"); out = open("other_speech.txt", "w")` - Read that tutorial again. – TigerhawkT3 Feb 25 '17 at 01:01
  • What? Also, why duplicate? I spent 50 minutes looking for a similar question, and I didn't find any – Monok Feb 25 '17 at 01:06
  • You need to review how variable names work. The duplicate explains how to work with files. – TigerhawkT3 Feb 25 '17 at 01:15
  • I was using a book to study, so I thought I didn't need to read another tutorial, after the book's one and the Python's one... – Monok Feb 25 '17 at 01:24

2 Answers2

2

when you open the file here:

out = open("man_speech.txt", "w")

You are assigning the file to the out variable, there's no such variable called man_speech. That's why it raises a NameError and says man_speech is not defined.

You need to change it to

man_speech = open("man_speech.txt", "w")

The same for other_speech

Vasili Syrakis
  • 9,321
  • 1
  • 39
  • 56
  • Oh...ahahahah, ok, I get it! So whenever I need to save data to a file I need to create a variable named exactly as my file? – Monok Feb 25 '17 at 01:04
  • No. You may call them file1 and file2 but then you need to use file1 and file2 to refer to them. `file1 = open("man_speech.txt", "w")` `print(man, file=file1)` `file1.close()` – Jérôme Feb 25 '17 at 01:07
  • You can name the variable anything you like, as long as it makes sense to you and your colleagues, and you use the same variable name to refer to it :) – Vasili Syrakis Feb 25 '17 at 01:13
1

There seems to be an issue with the file names:

out = open("man_speech.txt", "w")    # Defining out instead of man_speech
out = open("other_speech.txt", "w")  # Redefining out
print(man, file=man_speech)          # Using undefined man_speech
print(other, file=other_speech)      # Using undefined other_speech

You don't assign the result of open to man_speech but to out. Hence the error message:

NameError: name 'man_speech' is not defined

The code should be

man_speech = open("man_speech.txt", "w")
other_speech = open("other_speech.txt", "w")
print(man, file=man_speech)
print(other, file=other_speech)
Jérôme
  • 13,328
  • 7
  • 56
  • 106