標籤:python 模組初始
Python的強大之處在於他有非常豐富和強大的標準庫和第三方庫,幾乎你想實現的任何功能都有相應的Python庫支援,以後的課程中會深入講解常用到的各種庫,現在,我們先來象徵性的學2個簡單的。
#sys
#!/usr/bin/env python# -*- coding: utf-8 -*- import sys print(sys.argv) #輸出$ python test.py helo world[‘test.py‘, ‘helo‘, ‘world‘] #把執行指令碼時傳遞的參數擷取到了
#os
#!/usr/bin/env python# -*- coding: utf-8 -*- import os os.system("df -h") #調用系統命令
完全結合一下:
import os,sys os.system(‘‘.join(sys.argv[1:])) #把使用者的輸入的參數當作一條命令交給os.system來執行
自己寫個模組:
import sysimport readlineimport rlcompleterif sys.platform == ‘darwin‘ and sys.version_info[0] == 2: readline.parse_and_bind("bind ^I rl_complete")else: readline.parse_and_bind("tab: complete") # linux and python3 on macfor mac
#!/usr/bin/env python # python startup file import sysimport readlineimport rlcompleterimport atexitimport os# tab completion readline.parse_and_bind(‘tab: complete‘)# history file histfile = os.path.join(os.environ[‘HOME‘], ‘.pythonhistory‘)try: readline.read_history_file(histfile)except IOError: passatexit.register(readline.write_history_file, histfile)del os, histfile, readline, rlcompleterfor Linux
寫完儲存後就可以使用了
localhost:~ jieli$ pythonPython 3.5.2 (default, Oct 23 2015, 18:05:06)[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import tab
你會發現,上面自己寫的tab.py模組只能在目前的目錄下匯入,如果想在系統的何何一個地方都使用怎麼辦呢? 此時你就要把這個tab.py放到python全域環境變數目錄裡啦,基本一般都放在一個叫 Python/2.7/site-packages 目錄下,這個目錄在不同的OS裡放的位置不一樣,用 print(sys.path) 可以查看python環境變數列表。
本文出自 “小菜鳥” 部落格,請務必保留此出處http://baishuchao.blog.51cto.com/12918589/1933582
python 模組初始