0

For my python class I was instructed to create a function that will read a file and report the number of lines, words, and characters. I can get the code to work, but once I try to convert it to a function, it doesn't work. what's wrong? I also need to return the values in a tuple. I keep getting this error:UnboundLocalError: local variable 'line_cnt' referenced before assignment

def file_elem(filenm):
    f = open(filenm,'r')
    wrd_cnt = 0
    char_cnt = 0
    line_len = 0
    while f is open:
        line_cnt = len(f.readlines( ))
        for line in f:
            f_lines = line.split()
            wrd_cnt += len(f_lines)
            no_spaces = ''.join(line.split())
            char_cnt += len(no_spaces)
    return print(line_cnt, wrd_cnt, char_cnt)

import os
x = os.path.join("C:", "\\temp", "practice4.txt")
file_elem(x)
7
  • 1
    You are returning the result of the function print. You probably want to drop the print from the return line, and print the result outside the function. Commented Jun 23, 2013 at 19:14
  • 3
    By the way, does this code really works? while f is open looks very suspicious to me... (you are checking whether f and the global function open are the same object). Commented Jun 23, 2013 at 19:16
  • 3
    while f is open is now my new favourite bit of English which is syntactically valid Python but doesn't do at all what a beginner would expect.. Commented Jun 23, 2013 at 19:17
  • I second! while f is open should be a new PEP recommendation! Seems more natural than with open("file") as f! Commented Jun 23, 2013 at 19:18
  • I see, when I change the code to with open("file") as f, it does return the first value correctly, but gives 0 for the last two values Commented Jun 23, 2013 at 19:23

2 Answers 2

1

Change while f is open to with open(filenm,'r') as f and remove f = open(filenm,'r'). And move the return (line_cnt, wrd_cnt, char_cnt) inside the with block!

Also, you don't need line_cnt = len(f.readlines( )). You should use a counter and increment it. Otherwise the file would be read before your for line in f!

Sign up to request clarification or add additional context in comments.

1 Comment

def file_elem(filenm): f = open(filenm,'r') line_cnt = 0 wrd_cnt = 0 char_cnt = 0 line_cnt = len(f.readlines( )) with open(filenm) as f: for line in f: f_lines = line.split() wrd_cnt += len(f_lines) no_spaces = ''.join(line.split()) char_cnt += len(no_spaces) return (line_cnt, wrd_cnt, char_cnt) import os x = os.path.join("C:", "\\temp", "practice4.txt") print (file_elem(x))
1

Change this:

return print(line_cnt, wrd_cnt, char_cnt)

to

return (line_cnt, wrd_cnt, char_cnt)

and this

file_elem(x)

to:

print file_elem(x)

and it should work the same as before. Your function returns the values. Your main program prints them.

3 Comments

UnboundLocalError: local variable 'line_cnt' referenced before assignment
As Omri Barel said, your while loop won't really work as you expect it to, so line_cnt is not going to be defined, and that's why you get the error.
Guess the print was withing the while loop initially!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.