Python

    版本為 20:30, 20 Nov 2024

    到這個版本。

    返回到 版本存檔.

    查閱目前版本

    Python Samples

    更多文章;

    基本語法

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    TIP:

    %.1f :小數點一位

    if 條件判斷

    if len(sys.argv) == 1:
    
    if len(sys.argv) != 1: 
        dosomething()
    else:
        doother()
    
    if ....:
        ...
    elif:
        ...
    elif:
        ...
    else:
        ...
    

    字串(String)與列表(List)處理

    #字串切割(Slice)
    #從左邊索引時,字元編號從 0 開始
    print s[i]    #取字串的第i個字元
    print s[i:j]  #取字串的第i個開始到第j-1個字元
    print s[-1]   #取字串的最後一個字元
    print s[:j]   #取字串的第一個開始到第j-1個字元
    print s[i:]   #取字串的第i個開始的所有字元
    print s[::k]  #從整個字串裡,取出每隔k個的字串
    #從右邊索引,字元編號從 1 開始
    print s[-i]   #取字串的最後第i個字元 
    

    去除字串裡的空白

    # 去除左、右兩邊的空白
    s
    =" \t a string example\t " s = s.strip()

    #
    去除左邊的空白 s = s.lstrip() # 去除右邊的空白 s = s.rstrip() # 去除所有的空白、換行、TAB s = s.strip('\t\n\r')

    模組安裝

    setuptools 工具

    $ wget https://bootstrap.pypa.io/ez_setup.py
    $ sudo python ez_setup.py 

     

    查詢模組的使用方法

    python
    
    >>> import module-name
    >>> help(module-name) 
    >>> exit()
    

    異常處理方法(Exception)

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    取得 USB 隨身碟掛載路徑

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    取得檔案的建立時間

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    內建的 http server

    http://effbot.org/librarybook/simplehttpserver.htm

    # File: simplehttpserver-example-1.py
    
    import SimpleHTTPServer
    import SocketServer
    
    # minimal web server.  serves files relative to the
    # current directory.
    
    PORT = 8000
    
    Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
    
    httpd = SocketServer.TCPServer(("", PORT), Handler)
    
    print "serving at port", PORT
    httpd.serve_forever()
    

    建立一個檔案上傳的 Web-UI
    需要檔案: droppy.py
    假設要上傳檔案的儲存路徑為 ~/upload

    cd ~/upload
    python ~/bin/droopy.py -m "Hi, it's me Bob. You can send me a file."

    - h 可以顯示更多參數

    執行外部指令

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    取得 eth0 IP 位址

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

    檢查 eth0 是否 Up

    import urllib2
    import commands
    
    def internet_on():
        try:
            response=urllib2.urlopen('http://www.google.com', timeout=1)
            return True
        except urllib2.URLError as err: pass
        return False
    
    def eth_up(iface):
        state = commands.getoutput("cat /sys/class/net/%s/operstate" %iface)
        if state == "up":
            #print "Up."
            return True
        else:
            #print "Down."
            return False
    
    while True:
        if eth_up("eth0"):
            print "Connected."
        else:
            print "Disconnected."
    

    播放 IP 位址的語音

    reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefinedNameException' was thrown. (click for details)

     

    Powered by MindTouch Core