python 模組初始

來源:互聯網
上載者:User

標籤: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 模組初始

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.