2013-09-24 21 views
0

我想使用Pastebin爲我託管兩個文本文件,以允許我的腳本的任何副本通過互聯網更新自己。我的代碼正在工作,但生成的.py文件在每行之間添加了一個空行。這裏是我的腳本...如何從Pastebin輸出中刪除跳過的行?

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」位在工作時爲了方便。

我注意到用記事本打開生成的文件不會顯示跳過的線條,用寫字板打開時會產生混亂的混亂,而用空閒打開時會顯示跳過的線條。基於此,即使「原始」Pastebin文件似乎沒有任何格式,這似乎是格式問題。

編輯:我可以將所有空行刪除或保持原樣(沒有任何問題,但我已經注意到),但這會大大降低可讀性。

回答

1

嘗試在open()加入二元預選賽:

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

我注意到,你對引擎收錄文件是DOS格式,因此它在它\r\n。當您致電scriptFile.write()時,它會將\r\n轉換爲\r\r\n,這非常令人困惑。

指定"b"open()將導致scriptfile跳過翻譯和寫入文件是DOS格式。

或者,您可以確保pastebin文件中只有\n,並在腳本中使用mode="w"

+0

謝謝,這完全緩解了這個問題。你是如何確定pastebin文件的格式的? – womesiete

+1

使用我的瀏覽器,我將文件下載到我的Linux PC上。在shell中,我使用'file'或'vim'來確定文件類型(我不記得是哪一個)。 –