0

Using try and except, open animals_shortList.txt for reading and read the file data, creating a new list. Sort the list alphabetically. it would look like this temporary. https://i.stack.imgur.com/A7TE9.jpg

Once the list has been successfully created and sorted, loop through each item in the list and prints the animal, phylum, and diet, as shown. Use a variable to number each printed line. it should be shown like this [1]: https://i.stack.imgur.com/e9Wi5.jpg

Code:

import sys
def main():

names = []

phylum = []

diet = []

output = ""

infile = "animals_shortList.txt"

try:

    animalFile = open(infile, "r")


except:

    print("Must be file", infile)

animalList = infile.readlines()

print(animalList)

fileName.close()

main()

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
Dylan
  • 3
  • 2
  • Please edit the format of your post properly. you can use three backticks to format your code. – Aven Desta Mar 29 '20 at 17:03
  • Please don't post images of code, data, or Tracebacks. Copy and paste it as text then format it as code (select it and type `ctrl-k`) ... [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – wwii Mar 29 '20 at 17:15
  • Please format the code - select it and type `ctrl-k`. .. [Formatting help](https://stackoverflow.com/help/formatting) ... [more Formatting](https://stackoverflow.com/editing-help) ... [Formatting sandbox](https://meta.stackexchange.com/questions/3122/formatting-sandbox) – wwii Mar 29 '20 at 17:15
  • 1
    It isn't clear what you are having trouble with. Please read [mcve]. – wwii Mar 29 '20 at 17:17

1 Answers1

0

Try it like this :

infile = 'animals_shortList.txt'
try :
    with open (infile, 'r') as file :
        animalList = file.readlines ()
except :
    print ('Must be file ', infile)
print (animalList)
bashBedlam
  • 1,402
  • 1
  • 7
  • 11