2013-04-01 121 views
1

我想確定哪個版本的Python最適合Python腳本。有沒有一種自動化的方式來做到這一點?如何確定哪個版本的Python適合腳本?

的具體問題,我心目中是確定與這裏顯示的腳本(稱爲ea800_downloader.py)使用哪個版本的Python:

http://d.hatena.ne.jp/yamdan/20110129/1296320457

我問,因爲許多錯誤都在我試圖運行它。我試着用Python版本2.6和2.7來運行它。第一個錯誤的列表如下:

File "ea800_downloader.py", line 43 
    "" " 
    ^
SyntaxError: EOL while scanning string literal 

在這種情況下,這似乎是在一個多行註釋的嘗試,但有什麼地方出了問題的地方。任何意見,將不勝感激。

編輯:如建議,該腳本的代碼張貼。

#/Usr/bin/env python! 
    # - * - coding: UTF-8 - * - 

    # 
    # Ea800_downloader.Py 
    # 
    # Usage: python Ea800_downloader.Py folder_id OUT_DIR 
    #  
    # folder_id: ID of the folder that you want to download (see below) 
    # 
    # | ID | folder path | 
    # | ---- + ------- ----------------------------- | 
    # | 0 |/ETablet/var/ebook | 
    # | two |/ETablet/var/photos | 
    # | 4 |/ETablet/var/Enotes | 
    # | 8 |? | 
    # | 12 |/ETablet/etc/db | 
    # | fourteen |/ETablet/etc/template | 
    # | 31 |/ETablet | 
    # | 32 |/usr/local/ETablet/bin | 
    # | thirty-three |/ETablet/etc | 
    # | 34 |/ETablet/var | 
    # | 35 |/usr/local/ETablet/lib | 
    # | 37 |/usr/local/ETablet/bin/Ebookreader | 
    # 
    # OUT_DIR: the directory where you want to store the folder where you downloaded 
    to make two directories xmlfiles and files directly under the # OUT_DIR directory 
    to store the downloaded file, to # files directory 
    to # xmlfiles directory stores (in the form of XML) list of files. 


    Import sys 
    Import os 
    Import socket 
    Import Xml.Etree.ElementTree as etree 
    Import hashlib 


    BUF_SIZE = 8192 


    class Connection (object): 
     "" " 
     . Simple wrapper class for Raw TCP Connection 
     Refer to: Http://Docs.Python.Org/howto/sockets.Html 

     Attributes: 
     - _Sock 
     "" " 

     def __ init__ (self, address): 
      self._sock = socket.socket() 
      self._sock.connect (address) 

     def Send (self, op): 
      totalsent = 0 
      op = Op.Encode (" UTF-8 ") 
      op_size = len (op) 
      while Totalsent <Op_size: 
       sent = self._sock.send (op) 
       if sent == 0: 
        raise RuntimeError, \ 
         " socket Connection broken " 
       totalsent = totalsent + sent 

     def recv (self, size = one): 
      read = 0 
      buf = [] 
      while read <size: 
       data = self._sock.recv (BUF_SIZE) 
       if Data == "": 
        raise RuntimeError, \ 
         " socket Connection broken " 
       read = read + len (data) 
       buf.append (data) 

      Return "". join (buf) 

     def close (self): 
      self._sock.close() 


    class Downloader (object): 
     "" " 
     File downloader for Eee Note EA800. 

     Attributes: 
     - _cs 
     - _DS 
     - _Files_folder 
     - _Xmlfiles_folder 
     "" " 

     def __ init__ (self, OUT_DIR): 
      # Connect to command socket 
      Connection Self._Cs = ((" 169.254.2.1 ", twenty thousand)) 
      # recieve welcome message 
      self._cs.recv() 

      # Connect to Data socket 
      Connection Self._Ds = ((" 169.254.2.1 ", 20 001)) 
      # recieve welcome message 
      self._cs.recv() 

      Self._Files_folder = os.path.join (OUT_DIR, " Files ") 
      Self._Xmlfiles_folder = os.path.join (OUT_DIR, " Xmlfiles ") 

     def close (self): 
      # close connections both 
      self._ds.close() 
      self._cs.close() 

     def _Md5sum (self, Data): 
      m = hashlib.md5() 
      m.update (data) 
      Return M.Hexdigest() 

     def downloadfile (self, folder_id, path): 
      "" " EXECUTE 'downloadfile' command "" " 

      # Send "downloadfile" request 
      op = " downloadfile \ n % d \ n Path =% s \ n \ \ n :: \ \ n "% (folder_id, path) 
      self._cs.send (op) 

      # Receive "downloadfilereturnvalue" response 
      res = self._cs.recv() 
      Res.Split Res_list = (b " \ n ") 

      assert Res_list [0] == " Downloadfilereturnvalue "," not correct response " 

      # Extract status and file_size from "downloadfilereturnvalue" response 
      status = res_list [1] 
      file_size = int (res_list [2]) 
      MD5 = Res_list [three] [4:] # drop first four character "MD5 =" 

      assert file_size> 0, " file_size is not positive integer " 

      # Prepare file and directory to output data 
      outfile_path = os.path.join (self._files_folder, 
             " % 02d "% folder_id, 
             Path.Lstrip ("/")) 
      outfile_dir = os.path.dirname (outfile_path) 
      try : 
       os.makedirs (outfile_dir) 
      except OSError: 
       pass 

      # Output data to local directory 
      data = self._ds.recv (file_size) 
      assert Self._Md5sum (Data) == MD5, " invalid MD5 check sum " 
      outfile = Open (Outfile_path, " wb ") 
      outfile.write (data) 
      outfile.close() 

      Print " Downloaded:% s "% path 

     def listfile (self, folder_id): 
      "" " EXECUTE 'listfile' command "" " 

      # Send "listfile" request 
      op = " listfile \ n % d \ n \ \ n :: \ \ n "% folder_id 
      self._cs.send (op) 

      # Receive "listfilereturnvalue" response 
      res = self._cs.recv() 
      Res.Split Res_list = (b " \ n ") 

      assert Res_list [0] == " Listfilereturnvalue " 

      # Extract status and file_size from "listfilereturnvalue" response 
      status = res_list [1] 
      file_size = res_list [2] 
      if File_size.Endswith (" \ \ n :: \ \ n "): 
       file_size = file_size [: -6] 
      file_size = int (file_size) 

      assert file_size> 0 

      # Output data to console 
      data = self._ds.recv (file_size) 
      Print Data 

     def xmlfile (self, folder_id): 
      "" " EXECUTE 'xmlfile' command "" " 

      # Send "xmlfile" request 
      op = " xmlfile \ n % d \ n \ \ n :: \ \ n "% folder_id 
      self._cs.send (op) 

      # Receive "xmlfilereturnvalue" response 
      res = self._cs.recv() 
      Res.Split Res_list = (b " \ n ") 

      assert Res_list [0] == " Xmlfilereturnvalue " 

      # Extract status and file_size from "xmlfilereturnvalue" response 
      status = res_list [1] 
      file_size = res_list [2] 
      file_size = int (file_size) 

      assert file_size> 0 

      # Prepare output to File and Directory Data 
      Outfile_path = os.path.join (Self._Xmlfiles_folder, " % 02D.Xml "% folder_id) 
      try : 
       os.makedirs (self._xmlfiles_folder) 
      except OSError: 
       pass 

      # Output to local Data Directory 
      outfile = Open (Outfile_path, " wb ") 
      data = self._ds.recv (file_size) 
      outfile.write (data) 
      outfile.close() 

      # Parse xmlfile 
      tree = etree.parse (outfile_path) 

      Return tree 

     def Downloadfolder (self, folder_id): 
      "" " recursive download Files in a folder "" " 

      tree = self.xmlfile (folder_id) 
      self._traversal_and_download (tree.getroot(), u "", folder_id) 

     def _Traversal_and_download (self, Elm, folder_name, folder_id): 
      if Elm.Tag == " File ": 
       path = folder_name + u "/"+ Elm.Get (" name ") 
       try : 
        self.downloadfile (folder_id, path) 
       except AssertionError, e: 
        Print e 
      else : 
       for child in Elm.Getchildren(): 
        Self._Traversal_and_download (child, Elm.Get (" name ", u" "), folder_id) 


    if __ name__ == " __ main__ ": 
     folder_id = int (sys.argv [1]) 
     out_dir = sys.argv [2] 

     d = Downloader (out_dir) 
     d.downloadfolder (folder_id) 
     d.close() 
+1

我認爲這裏有一個不需要的雙引號''''檢查一下,或者發佈代碼 –

+0

「」「 –

+0

@JoranBeasley有一個空格:代碼 –

回答

1

它看起來像複製粘貼錯誤或編碼問題。我去了原來的網站,在Python 2.7下運行得很好。你只需要修復三個雙引號之間的空格。

要回答您的原始問題,您可以編寫檢查語法腳本,該腳本讀取代碼以查看哪些版本的Python可以成功編譯代碼。如果你可以運行compile(somescript, '', 'exec'),那麼在當前版本的Python下代碼是否正常。這是IDLE's checksyntax() function使用的策略。

+0

是的,我想我做了一個愚蠢的複製和粘貼錯誤,引入空格和縮進應該沒有。腳本現在正在運行。謝謝您的幫助。 – d3pd

1

該代碼似乎有語法錯誤。 "" "在任何版本的Python中都不是有效的語法。您需要修復代碼或嘗試重新加載它。試用其他版本的Python將無濟於事。很可能你需要刪除引號之間的空格。如果您複製並粘貼了代碼,則可能需要修改該格式。

+0

感謝您的協助。看來我做了一個愚蠢的複製和粘貼錯誤。 – d3pd

相關問題