0

Hi

Ive got this word count script when i check it there is no probs but when i run it it says

Traceback (most recent call last):
File "H:\Scripts\count.py", line 9, in <module>
infile = file(data, 'r')
NameError: name 'data' is not defined

This is my script

#The number of lines, and the number of words.

import string

def main():
data = raw_input("H:\harry.txt")
infile = file(data, 'r')
data_file = infile.read()
number_of_characters = len(data_file)
print "The number of characters in your text is", number_of_characters
list_of_words = string.split(data_file)
number_of_words = len(list_of_words)
print "The number of words in your text is", number_of_words
infile.close()
secondfile = file(data, 'r')
line_of_text = secondfile.readlines()
print line_of_text
number_of_lines = len(lines_of_text)
print "The number of lines in your text is" , number_of_lines
infile.close()
main()

can anybody tell me what is wrong with line 9

Thanks

HLA91

STAY COOL AND DONT GET STRESSED!:cool:

2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by bumsfeld
1

The two functions
num = input("Enter a number: ")
and
name = raw_input("Enter a name: ")
are for the user to enter number or name from the console.

Please use code blocks.

import string

def main():
    #data = raw_input("H:\harry.txt")  # use data = raw_input("Enter file name: ")
    # what you want is most likely this
    # expects the file to be in the working/current directory
    data = "Harry.txt"
    infile = file(data, 'r')
    data_file = infile.read()
    number_of_characters = len(data_file)
    print "The number of characters in your text is", number_of_characters
    list_of_words = string.split(data_file)
    number_of_words = len(list_of_words)
    print "The number of words in your text is", number_of_words
    infile.close()
    secondfile = file(data, 'r')
    line_of_text = secondfile.readlines()
    print line_of_text
    number_of_lines = len(line_of_text)  # corrected lines_of_text to line_of_text
    print "The number of lines in your text is" , number_of_lines
    infile.close()

main()

Edited by pyTony: fixed formatting

Votes + Comments
very helpfull
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.