如何从Pastebin输出中删除跳过的行?

时间:2013-09-24 06:46:34

标签: python auto-update pastebin

我正在尝试使用Pastebin为我托管两个文本文件,以允许我的脚本的任何副本通过互联网更新自己。我的代码正在运行,但结果.py文件在每一行之间添加了一个空行。这是我的剧本......

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()

我剥离了GUI(wxpython)并将脚本设置为更新另一个文件而不是实际运行的文件。 “No_Update”位是为了方便工作。

我注意到用记事本打开结果文件并没有显示跳过的行,用Wordpad打开会产生乱七八糟的混乱,而用Idle打开会显示跳过的行。基于此,即使“原始”Pastebin文件似乎没有任何格式,这似乎是格式化问题。

编辑:我可以删除所有空行或保持原样,没有任何问题,(我已经注意到了)但这会大大降低可读性。

1 个答案:

答案 0 :(得分:1)

尝试在open()中添加二元限定符:

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

我注意到你在pastebin上的文件是DOS格式的,所以它里面有\r\n。当您致电scriptFile.write()时,它会将\r\n翻译为\r\r\n,这非常令人困惑。

"b"中指定open()将导致脚本文件跳过该转换并将文件写为DOS格式。

或者,您可以确保pastebin文件中只包含\n,并在脚本中使用mode="w"

相关问题