this doesnt work when the file is in the same directory as the .py file.

def get_colocations(filename):
           sentences = open(filename).read().split("\n")
           colocations = []
          for sent in sentences:
                colocations.append(sent)
                return colocations

def main():
    get_colocations("colocations.txt")
    print "Colocations are",colocations

Can anyone help????????

Indentations are the bread and butter of Python. Yours are all messed up! Did you mix tabs and spaces in your code? Avoid the mix!

This is corrected code:

def get_colocations(filename):
    sentences = open(filename).read().split("\n")
    colocations = []
    for sent in sentences:
        colocations.append(sent)
    return colocations

def main():
    colocations = get_colocations("colocations.txt")  # !!!!!!!!!
    print "Colocations are",colocations

main()

thanks a lot bumsfield....i need to indent my whole program :(
EDIT:i tried the above code but it doesnt work..the program compiles but doesnt return the colocations list...the colocations.txt file is in the same directory as this file..what could be the error?

This question has already been answered. Start a new discussion instead.