0

I am fairly new to python and I am trying to figure out why my functions aren't giving me the output I am looking for. I feel like I have tried everything and tried researching, but I haven't found anything that has helped me.

Here is my code:

from sys import argv                    

def GetCharactersWords(input):
    characters = input.read()
    words = len(characters.split())              
    #word = len(words)
    return words

def GetCharacterNumber(input):
    characters = input.read()
    number = characters.count(".")       
    return number                            

def FirstSentence(input):
    characters = input.read()           
    fields = characters.split(".")        
    field1 = fields[0]                  
    return field1


def LengthFirstSentence(input):
    characters = input.read()
    fields = characters.split(".")
    field1 = fields[0]
    field0 = len(field1)
    return field0

def LastSentence(input):
    characters = input.read()
    fields = characters.split(".")
    field2 = fields[-1]
    return field2                  


def LengthLastSentence(input):
    characters = input.read()
    fields = characters.split(".")
    field3 = fields[-1]                  
    field4 = len(field3)
    return field4


def print_to_file(word_characters, number_characters, first_sentence, length_first,
last_sentence, length_last):
    with open("output.txt", "w") as outfile: 
        outfile.write("Here is the text file: " + '\n') 
        outfile.write(str(input) + '\n')               
        outfile.write("Number of words in the file?" + '\n')  
        outfile.write(str(word_characters) + '\n')
        outfile.write("Number of sentences in the file?" + '\n')
        outfile.write(str(number_characters) + '\n')
        outfile.write("First sentence in the file?" + '\n')
        outfile.write(str(first_sentence) + '\n')
        outfile.write("Last sentence in the file?" + '\n')
        outfile.write(str(last_sentence) + '\n')
        outfile.write("Length of the first sentence" + '\n')
        outfile.write(str(length_first)+ '\n')
        outfile.write("Length of the last sentence" + '\n')
        outfile.write(str(length_last) + '\n')
        outfile.close()                             



def main(argv):                         
    script = argv[0]                    
    filename = argv[1]                  
    input = open(filename, "r")       
    word_characters = GetCharactersWords(input)
    number_characters = GetCharacterNumber(input)
    first_sentence = FirstSentence(input)
    length_first = LengthFirstSentence(input)
    last_sentence = LastSentence(input)
    length_last = LengthLastSentence(input)
    file_print = print_to_file(word_characters, number_characters, first_sentence, length_first,
    last_sentence, length_last)



if __name__ == '__main__':
        main(argv)

Here is the output it gives me:

Here is the text file: 
<built-in function input>
Number of words in the file?
2430
Number of sentences in the file?
0
First sentence in the file?

Last sentence in the file?

Length of the first sentence
0
Length of the last sentence
0

I can't figure out why it isn't giving me the different outputs.

Thanks for any help!

  • 2
    Each of your functions reads the entire file, leaving the current file position at the very end of the file - so every function you call after the first one has nothing to read. Either reopen the file each time, rewind it to the start via `.seek(0)`, or read the file once in `main()` and pass the contents to each function. (Note that `input` was a bad name for the file, as that's overriding a Python built-in function). – jasonharper Oct 07 '20 at 03:12
  • Does this answer your question? [Python: Why a file is empty after reading it and writing to another one?](https://stackoverflow.com/questions/28320658/python-why-a-file-is-empty-after-reading-it-and-writing-to-another-one) – wjandrea Oct 07 '20 at 03:14
  • or these? [Re-open files in Python?](https://stackoverflow.com/q/2106820/4518341) [Why can't I call read() twice on an open file?](https://stackoverflow.com/q/3906137/4518341) – wjandrea Oct 07 '20 at 03:14
  • 1
    BTW, welcome to SO! Check out the [tour], and [ask] if you want advice. For example, making a [mre] is super helpful. You posted a lot of code, most of which is not relevant to the problem. – wjandrea Oct 07 '20 at 03:16

1 Answers1

1

There are a few problems with your code. The biggest one is that you're not reading the file properly. When you open a file with open(), you can only read() the file once. Calling read() will read the entire file and empty the stream, so a second call to read() will only give you an empty string, which is why only the "Number of words" has a non-empty result.

Instead, you should call read() once to read the contents, and pass the contents of the file directly instead of the file handle to your functions. For example:

# Take characters directly instead of a file
def FirstSentence(characters):
    fields = characters.split(".")        
    field1 = fields[0]                  
    return field1

def main(argv):                         
    script = argv[0]                    
    filename = argv[1]                  
    characters = open(filename, "r").read()
    word_characters = GetCharactersWords(characters) # and so on

Additionally, "<built-in function input>" is printed for the file contents because when you printed it, input has not been assigned, so it ends up printing the built-in function input instead. As others have pointed out, input is a bad variable name as it overrides the built-in input function. If you had chosen some other name instead, it would have resulted in a NameError.

Tyler Tian
  • 597
  • 1
  • 6
  • 22