'Creating a function to sort particular data in a text.txt

I am trying to sort out the first three elements in a text file using a function. I was able to do it correctly with some simpler code but I am not sure why it won't work within the function. It won't update my empty list I created for some reason. But in the working code, that isn't a function, it works just fine.

This is the working code:

mytags = []
mylines = []
with open ('iphone_07_18_1.annot', 'r') as myfile:
    for line in myfile:
        mylines.append(line)
    for element in mylines:
        mytags.append(element[:3])
print(mytags)

This is my function that isn't working:

#Open&close files, read file and set the variables.
fhand = open('iphone_07_18_1.annot', 'r')
myfile = fhand.read()
mylines = []
mytags = []
fhand.close()

def Tags():     #function to create a list to append from
    for line in myfile:
         mylines.append(line)
    for element in mylines:
         mytags.append(element[:3])
    print(mytags)
Tags()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source