Dear friends,
I need help understanding my error with urllib.

I wrote the following code to check the availability of updates for a software I wrote

try:
    datasource = urllib.urlopen("URL to FILE.txt")
        while 1:
            line = datasource.readline()
                if line == "": break
                if (line.find("<") > -1) :
                    LastVersion="Error"
            else:
                LastVersion=line

            if LastVersion=="Error":
                do something

            elif version.VERSION==LastVersion:
                do somethingelse

            else:
                versioncheck = wx.MessageDialog(None,"It seems that a new version [v. "+LastVersion+"] is available!","MySoftware "+version.VERSION+"", wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP)
                versioncheck.ShowModal()

except Exception, err:
    versioncheck = wx.MessageDialog(None,"An error has occurred in version checking. Please, try again later or contact the author","MySoftware "+version.VERSION+"", wx.OK | wx.ICON_ERROR | wx.STAY_ON_TOP)
    versioncheck.ShowModal()

Now, I don't understand why the urllib.urlopen("URL to FILE.txt") returns the last line of that txt file and not the first (as I need).
Can you help me understand? And how can I fix it?

Thanks,
Gianluca

returns the last line of that txt file and not the first (as I need).

If you just need first line why make it more difficult than it is.
just readline() give back first line.
If need line by line drop while 1 and just iterate over lines with a for loop and grab line you want.
A coulpe of example here i use open() but you can also iterate over urlopen() in a similar way.

"""latest.txt-->
version 2
version 5 latest
version 3
"""

with open('latest.txt') as f:
    for line in f:
        if 'latest' in line:
            print line.strip() #The line you want
            print 'The latest version is "{}"'.format(line[:9]) #The current version

"""Output-->
version 5 latest
The latest version is "version 5"
"""

Just first line,if lastest version is always there.

with open('latest.txt') as f:
    first_line = f.readline()
    print first_line.strip() #--> version 2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.