- 2

If i want to code this using while loop!! how would it seem like??

#Already those txt files are with me so dont worry just answer!! contents = ['alice.txt', 'cindrella.txt', 'budhha.txt', 'fake.txt'] for content in contents: try: with open(content) as bio: bios = bio.read() list_bio = bios.split() len_bio = len(list_bio) except FileNotFoundError: print('The text file', content, 'donot exist in the current folder!! \r\n') else: print('The length of ' + content + ' is ' + str(len_bio) + '\r\n')

20th May 2020, 10:36 AM
Lucky Nayak
Lucky Nayak - avatar
3 Answers
20th May 2020, 12:44 PM
Priyal Vyas
Priyal Vyas - avatar
+ 1
Make a counter thats the length of the contents. Then the while loop should be while counter and each time you read a file subtract one from counter. when counter is 0, it will stop
20th May 2020, 10:45 AM
Slick
Slick - avatar
0
#i found the answer!! contents = ['alice.txt', 'cindrella.txt', 'budhha.txt', 'fake.txt'] counter = 0 i = 0 while counter <= len(contents) - 1: try: with open(contents[counter]) as content: bio = content.read() bio_split = bio.split() bio_len = len(bio) except FileNotFoundError: print('Your file', contents[counter], 'is not found', '\n') else: print('Length of your file ' + contents[counter] + ' is ' + str(bio_len) + '\r\n') counter += 1
20th May 2020, 2:43 PM
Lucky Nayak
Lucky Nayak - avatar