6 Replies - 2171 Views - Last Post: 28 March 2011 - 04:25 PM Rate Topic: -----

#1 CodyD  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 75
  • Joined: 19-August 10

Downloading files using URLLib/URLLib2?

Posted 27 March 2011 - 11:23 AM

I'm trying to implement a "Check for Updates..." option in my program, and I'm trying to download a file from the internet called "updaterelease.txt" which has the following in it:
vx.x


Python should download this, place it in the directory where the program is running, read the file to see what x.x is (say the program version is 1.0, and the latest is 1.1, it should read till 1.1)

A messagebox should pop up saying: "A more recent version of Pivot: Revolution is available. Please go to (url)"

Here is my current code for it:
import urllib
appversion = "v0.1"

    def updateDlg(self, event):
        updateplace = "https://sourceforge.net/projects/pivotrevolution/files/"
        updatefile = "updaterelease.txt"
        """Copy the contents of a file from a given URL
        to a local file.
        """
        webFile = urllib.urlopen(updateplace + updatefile)
        localFile = open((updateplace + updatefile).split('/')[-1], 'w')
        localFile.write(webFile.read())
        webFile.close()
        dlfile = str(localFile)
        file = open(dlfile)
        version = file.read()
        if version > appversion:
            updateMsgBox = wx.MessageBox("A more recent version of Pivot: Revolution is available! Please go to" + updateplace + 
                "developement_release/" + version + "/download to download", "Update available!", style = wx.OK)
        localFile.close()
        





I get the following error:
Traceback (most recent call last):
File "/home/cody/Pivot: Revolution/RunMe.py", line 130, in updateDlg
file = open(dlfile)
IOError: [Errno 2] No such file or directory: "<open file 'updaterelease.txt', mode 'w' at 0x9b68230>"
Script terminated.

This post has been edited by CodyD: 27 March 2011 - 11:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Downloading files using URLLib/URLLib2?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: Downloading files using URLLib/URLLib2?

Posted 27 March 2011 - 01:31 PM

    webFile = urllib.urlopen(updateplace + updatefile)
    localFile = open((updateplace + updatefile).split('/')[-1], 'w')
    localFile.write(webFile.read())
    webFile.close()
    dlfile = str(localFile)


localFile is a File Object, not something you can successfully cast to a string.
Was This Post Helpful? 0
  • +
  • -

#3 CodyD  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 75
  • Joined: 19-August 10

Re: Downloading files using URLLib/URLLib2?

Posted 27 March 2011 - 07:09 PM

Thank you very much! The code is working now. Mostly.

    def updateDlg(self, event):
        updateplace = "https://sourceforge.net/projects/pivotrevolution/files/"
        updatefile = "updaterelease.txt"
        """Copy the contents of a file from a given URL
        to a local file.
        """
        webFile = urllib.urlopen(updateplace + updatefile)
        localFile = open((updateplace + updatefile).split('/')[-1], 'w')
        localFile.write(webFile.read())
        webFile.close()
        file = open("updaterelease.txt", "r")
        version = file.read()
        appversion = 0.1
        
        if version > appversion:
            wx.MessageBox("A more recent version of Pivot: Revolution is available! The latest version is " + str(version) +
            "Please go to " + updateplace + "developement_release/" + str(version) + "/download to download", "Update available!", style = wx.OK)
        
        elif version == appversion:
            wx.MessageBox("You have the latest and greatest version! Enjoy!", "No update available.", style = wx.OK)
            
        else:
            wx.MessageBox("There's something wrong here. You have a later version than is available.", "You have a newer version than available.", style = wx.OK)
        localFile.close()



The only problem: The version does not show up in the message box.
wx.MessageBox("A more recent version of Pivot: Revolution is available! The latest version is " + str(version) + "Please go to " + updateplace + "developement_release/" + str(version) + "/download to download", "Update available!", style = wx.OK)


updateplace shows up. However, regardless of whether I type "str(version)", or "version", etc... it does not show up. :\
Was This Post Helpful? 0
  • +
  • -

#4 CodyD  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 75
  • Joined: 19-August 10

Re: Downloading files using URLLib/URLLib2?

Posted 27 March 2011 - 09:13 PM

Nevermind, apparently that doesn't work.

It reads that 0.1 > 0.1.... which isn't true.

What's going on here?

My code is in my previous post, it hasn't changed at all.

:(
Was This Post Helpful? 0
  • +
  • -

#5 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: Downloading files using URLLib/URLLib2?

Posted 28 March 2011 - 04:48 AM

You might need to swap your if statements around, so your code would look like this:
def updateDlg(self, event):
    updateplace = "https://sourceforge.net/projects/pivotrevolution/files/"
    updatefile = "updaterelease.txt"
    """Copy the contents of a file from a given URL
    to a local file.
    """
    webFile = urllib.urlopen(updateplace + updatefile)
    localFile = open((updateplace + updatefile).split('/')[-1], 'w')
    localFile.write(webFile.read())
    webFile.close()
    file = open("updaterelease.txt", "r")
    version = file.read()
    appversion = 0.1
    
    if version == appversion:
        wx.MessageBox("You have the latest and greatest version! Enjoy!", "No update available.", style = wx.OK)
    
    elif version > appversion:
        wx.MessageBox("A more recent version of Pivot: Revolution is available! The latest version is " + str(version) +
        "Please go to " + updateplace + "developement_release/" + str(version) + "/download to download", "Update available!", style = wx.OK)
    
   
        
    else:
        wx.MessageBox("There's something wrong here. You have a later version than is available.", "You have a newer version than available.", style = wx.OK)
    localFile.close()


It seems weird to me that Python is doing that, but it seemed to work for me in my test when I checked if they were equal first.
Good luck!
Was This Post Helpful? 0
  • +
  • -

#6 CodyD  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 75
  • Joined: 19-August 10

Re: Downloading files using URLLib/URLLib2?

Posted 28 March 2011 - 03:27 PM

from wx import *
import urllib

def updateDlg(self):
    updateplace = "https://sourceforge.net/projects/pivotrevolution/files/"
    updatefile = "updaterelease.txt"
    """Copy the contents of a file from a given URL
    to a local file.
    """
    webFile = urllib.urlopen(updateplace + updatefile)
    localFile = open((updateplace + updatefile).split('/')[-1], 'w')
    localFile.write(webFile.read())
    file = open("updaterelease.txt", 'r')
    webFile.close()
    version = file.read()
    appversion = 0.1
    
    if version == appversion:
        wx.MessageBox("You have the latest and greatest version! Enjoy!", "No update available.", style = wx.OK)
        
    elif version > appversion:
        wx.MessageBox("A more recent version of Pivot: Revolution is available! The latest version is " + file.readline() +
        ". Please go to " + updateplace + "developement_release/" + str(version) + "/download to download.", "Update available!", style = wx.OK)
    
    else:
        wx.MessageBox("There's something wrong here. You have a later version than is available.", "You have a newer version than available.", style = wx.OK)
    localFile.close()



The file on the server says "0.1" on it.
The appversion says 0.1

It still doesn't work. It says a newer version is available, when 0.1 is equal to 0.1, not greater than 0.1 :)

This post has been edited by CodyD: 28 March 2011 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: Downloading files using URLLib/URLLib2?

Posted 28 March 2011 - 04:25 PM

Thesis:
version = file.read()

version here would likely be a string.

appversion = 0.1

appversion here would likely be a float-type value

Proof:
#!/usr/bin/env python

with open('testfile', 'r') as f:
    fileval = f.read()

floatval = 0.1

print floatval == fileval
print floatval == float(fileval)



Contents of testfile:
0.1


Result:
False
True


Thesis proven.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1