-2

I'm currently trying to return the length of the longest line in a given string. Currently, my output is just reading "1". Help? This is what I have so far.

def longest_line_length(file_name):
    print(max(len(line) for line in file_name))
def main():
    print(longest_line_length('poem.txt'))
if __name__ == '__main__':
    main()
heth123
  • 31
  • 4
  • Is poem.txt a file that you want to open or is it the actual string? – Mitchell Olislagers Jan 18 '21 at 01:50
  • The file I want to open. – heth123 Jan 18 '21 at 01:51
  • Try removing len() – Mick Jan 18 '21 at 01:53
  • 1
    Then you will first need to open it before you can find the longest string. – Mitchell Olislagers Jan 18 '21 at 01:53
  • 1
    try this ```print(line) for line in file_name``` It will be ["p", "o", "e", "m", ".", "t", "x", "t"] and this should help point out your issue – questionerofdy Jan 18 '21 at 01:54
  • Well I want it to return the length of the longest line in poem.txt (which happens to be 35). "poem.txt" isn't the actual string itself – heth123 Jan 18 '21 at 01:55
  • @questionerofdy actually, it will print: 1, 1, 1, 1, 1, 1, 1 – Mick Jan 18 '21 at 01:56
  • @Mick sorry I edited the comment a bunch Look at this answer to see how to read a txt file and read into a string https://stackoverflow.com/questions/8369219/how-to-read-a-text-file-into-a-string-variable-and-strip-newlines – questionerofdy Jan 18 '21 at 01:56
  • You're not actually opening the file; you're just operating on the file _name_. – John Gordon Jan 18 '21 at 02:06
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). We expect you to check the operation of your program, and to work through applicable tutorials before you post a question. Stack Overflow is not intended to replace existing tutorials and documentation. – Prune Jan 18 '21 at 02:23

3 Answers3

0

The problem is with you did not read the file. First we have to open the desired file to be red and go through the lines (reading). Then we can find the length of the particular lines. Here I have written a simple snippet. Please check whether this works for you.

def txtFile_reader(txtFile_location):
    length_list = []
    with open(txtFile_location, 'r') as txt_file:
        for i, line in enumerate(txt_file.readlines(), 1):
            length_list.append((len(line.strip()), i))
    return max(length_list)[1]

if __name__ == '__main__':
    main()

This returns which line contains the longest string, if you need to see the string count as wellm then return max(length_list) . It will return a tupple containing first element denoting the string count followed by the line number. (xxx, xx)

nectarBee
  • 401
  • 3
  • 9
0

You just need to add two lines to your code.

def longest_line_length(file_name):
    data_file = open(file_name, "r")
    lst = [line.rstrip("\n")for line in data_file]
    return (max(len(line) for line in lst))
def main():
    print(longest_line_length('poem.txt'))
if __name__ == '__main__':
    main()
pakpe
  • 5,391
  • 2
  • 8
  • 23
0

If you want the line

def longest_line_length(file_name):
    with open(file_name) as file:
        return max([line for line in file])

If you want the length

def longest_line_length(file_name):
    with open(file_name) as file:
        return max([len(line) for line in file])
Mick
  • 796
  • 4
  • 8
  • I think there must be a syntax here (in the one for the length), as it's telling me there's an error at `def main():` in my program, when there hadn't been before and nothing changed. – heth123 Jan 18 '21 at 02:06
  • @heth123 Weird, it works fine for me. What error does it throw? – Mick Jan 18 '21 at 02:07
  • File "/home/main.py", line 17 def main(): ^ SyntaxError: invalid syntax – heth123 Jan 18 '21 at 02:09
  • @heth123 Oh lol, I was missing a parenthesis at the end – Mick Jan 18 '21 at 02:09