(python) The need for len

Asked 2 years ago, Updated 2 years ago, 126 views

fname = input('Enter fline name: ')
if len(fname) < 1 : fname = 'mbox-short.txt'

fh = open(fname)
count = 0

for line in fh :
    if 'From ' in line :
        mail = line.split()
        print(mail[1])
        count = count + 1
    else : continue

print('There were', count, 'lines in the file with From as the first word')

This is the code I saw while studying. I'm not sure how Len's presence or absence affects this code. There was no change even if I printed it out after annotating it... Why does it exist? Please give me some advice

python len

2022-09-22 19:37

1 Answers

This line is

Write "mbox-short.txt" as the default input if the input length is 0 or less (= if there is no input value).

I think it's a line that goes in that sense.

Comment on the line, rename the mbox-short.txt file, and try again without any input. open(fname) will probably pop.


2022-09-22 19:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.