Using python to read text files and answer questions
Question: I have this file animallog1.txt which contains information that i would like
to use to answer questions using python. How would i import the file to
python, which would latter be used I tried

    
    
    with open('animallog1.txt', 'r') as myfile
    

but this is not working and just outputs no such file or directory even though
i am pretty sure it exists

    
    
    animal_names, dates, locations = [], [], []
    
    with open('animallog1.txt', 'r') as myfile:
        for line in myfile:
            animal_name, date, location = line.strip().split(':')
            animal_names.append(animal_name)
            dates.append(date)
            locations.append(location)
    
    
     print(animal_names)
     print(dates)
     print(locations)
    

so this is the code that i have. animallog1.txt is the name of the file that i
want to use.

However my output is

    
    
    Traceback (most recent call last):
      File "None", line 3, in <module>
    builtins.FileNotFoundError: [Errno 2] No such file or directory: 'animallog1.txt'
    

how can i fix this?


Answer: Make sure you path is corret or try this:

    
    
    file = open("sample.txt")
    

sample.txt is present in the current path, in my ubuntu it works.

