Question

I am trying to use Pastebin to host two text files for me to allow any copy of my script to update itself through the internet. My code is working, but the resultant .py file has a blank line added between each line. Here is my script...

import os, inspect, urllib2

runningVersion = "1.00.0v"
versionUrl = "http://pastebin.com/raw.php?i=3JqJtUiX"
codeUrl = "http://pastebin.com/raw.php?i=GWqAQ0Xj"
scriptFilePath = (os.path.abspath(inspect.getfile(inspect.currentframe()))).replace("\\", "/")

def checkUpdate(silent=1):
    # silently attempt to update the script file by default, post messages if silent==0
    # never update if "No_Update.txt" exists in the same folder
    if os.path.exists(os.path.dirname(scriptFilePath)+"/No_Update.txt"):
        return
    try:
        versionData = urllib2.urlopen(versionUrl)
    except urllib2.URLError:
        if silent==0:
            print "Connection failed"
        return
    currentVersion = versionData.read()
    if runningVersion!=currentVersion:
        if silent==0:
            print "There has been an update.\nWould you like to download it?"
        try:
            codeData = urllib2.urlopen(codeUrl)
        except urllib2.URLError:
            if silent==0:
                print "Connection failed"
            return
        currentCode = codeData.read()
        with open(scriptFilePath.replace(".py","_UPDATED.py"), mode="w") as scriptFile:
            scriptFile.write(currentCode)
        if silent==0:
            print "Your program has been updated.\nChanges will take effect after you restart"
    elif silent==0:
        print "Your program is up to date"

checkUpdate()

I stripped the GUI (wxpython) and set the script to update another file instead of the actual running one. The "No_Update" bit is for convenience while working.

I noticed that opening the resultant file with Notepad does not show the skipped lines, opening with Wordpad gives a jumbled mess, and opening with Idle shows the skipped lines. Based on that, this seems to be a formatting problem even though the "raw" Pastebin file does not appear to have any formatting.

EDIT: I could just strip all blank lines or leave it as is without any problems, (that I've noticed) but that would greatly reduce readability.

Was it helpful?

Solution

Try adding the binary qualifier in your open():

with open(scriptFilePath.replace(".py","_UPDATED.py"), mode="wb") as scriptFile:

I notice that your file on pastebin is in DOS format, so it has \r\n in it. When you call scriptFile.write(), it translates \r\n to \r\r\n, which is terribly confusing.

Specifying "b" in the open() will cause scriptfile to skip that translate and write the file is DOS format.

In the alternative, you could ensure that the pastebin file has only \n in it, and use mode="w" in your script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top