0

Pastebin を使用して 2 つのテキスト ファイルをホストし、スクリプトのコピーをインターネット経由で更新できるようにしようとしています。私のコードは機能していますが、結果の .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」ビットは、作業中の便宜のためです。

結果のファイルをメモ帳で開いてもスキップされた行が表示されず、ワードパッドで開くとごちゃごちゃになり、Idleで開くとスキップされた行が表示されることに気付きました。それに基づいて、「生の」Pastebin ファイルにはフォーマットがないように見えますが、これはフォーマットの問題のようです。

編集:すべての空白行を削除するか、問題なくそのままにしておくことができますが(気づいたことです)、読みやすさが大幅に低下します。

4

1 に答える 1

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()と、scriptfile はその変換をスキップし、ファイルが DOS 形式であると書き込みます。

別の方法として、pastebin ファイルがその中にのみ含まれていることを確認し、スクリプト\nで使用することもできます。mode="w"

于 2013-09-26T03:46:03.487 に答える