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

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

            if LastVersion=="Error":
                do something

            elif version.VERSION==LastVersion:
                do somethingelse

                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)

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)

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?


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.

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

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

Edited 3 Years Ago by snippsat

This article has been dead for over six months. Start a new discussion instead.