8.2,常用模組介紹:sys,os,time,random,8.2random

來源:互聯網
上載者:User

8.2,常用模組介紹:sys,os,time,random,8.2random

 

sys:

介紹:主要包含涉及python編譯器與系統互動的函數。

常用函數:

import sysprint(sys.argv)#本檔案名稱,已經運行該程式時的參數#[如在命令視窗中python3 mysys.py 參數1 參數2]#那麼參數1為sys.argv[1],以此類推print(sys.version)#python版本號碼print(sys.path)#返回模組的搜尋路徑,初始化時使用PYTHONPATH環境變數的值# sys.exit(0)#中途退出程式,當參數非0時,會引發一個SystemExit異常

 

sys.stdout.write()#在螢幕中列印sys.stdout.flush()#重新整理標準緩衝區
os:

介紹:這個模組提供了一種方便的使用作業系統函數的方法。

常用函數:

import osprint("-------getcwd()擷取目前的目錄-------")print(os.getcwd())print("-------chdir()改變當前所在目錄-------")# print(os.chdir("c://users"))#c:\\users   r'c:\users'# print(os.getcwd())print("------ . .. --------")print(os.curdir)     #列印出 .print(os.pardir)     #列印出 ..print("-------makedirs遞迴建立目錄-------")#os.makedirs(r"c:\a\b\c")     #要建立c,如果a不存在則建立a,如果b不存在則建立bprint("-----remodir遞迴刪除目錄---------")#os.removedirs(r"c:\a\b\c")    #清除空檔案夾,從c到a,如果a,b也是空的話也會刪除。print("------mkdir建立目錄--------")# os.mkdir('c://a')print("--------listdir列出指定目錄下的所有檔案和子目錄------")print(os.listdir())print("--------remove刪除檔案------")# print(os.remove('c://newfile'))print("-------rename檔案重新命名-------")# os.rename('oldname','newname')print("-------stat 擷取檔案或目錄資訊-------")print(os.stat('.'))print("------sep 輸出作業系統特點的路徑分割符--------")print(os.sep)print("-----linesep 輸出當前平台的行終止符---------")list1=[]list1.append(os.linesep)print(list1)print("------pathsep 輸出用於分割檔案的字串--------")print(os.pathsep)print("----------name輸出操作平台----------")# print(os.name)#ntprint("-------system執行shell命令-------------")print(os.system("dir"))print("----------path關於檔案和目錄的操作----------")# print(os.path.abspath(__file__))###返回絕對路徑print(os.path.split(os.path.abspath(__file__)))##將路徑切割成目錄名和檔案名稱print(os.path.dirname(os.path.abspath(__file__)))#只取路徑名print(os.path.dirname(__file__))###__file__是包括完整路徑名的,也是絕對路徑print(os.path.basename(__file__))#只取檔案名稱print(os.path.exists("c://a"))#判斷路徑是否存在,不區分目錄或檔案print(os.path.isabs(__file__))#判斷是否是絕對路徑print(os.path.isfile("c://amd"))#判斷是否是檔案print(os.path.join(r'c:',r'\a.txt'))#組合絕對路徑print("----------environ擷取當前系統所有環境變數----------")print(os.environ)print("---------popen() 方法用於從一個命令開啟一個管道-----------")print(os.popen('dir').read())##主要用於處理執行命令的返回結果print("擷取進程號".center(50,'-'))print(os.getpid())#擷取當前進程號print(os.getppid())#擷取父進程號

注意:

在某些系統中,os.system跟os.popen的主要區別是前者傳回值是指令碼的退出狀態代碼,後者的傳回值是指令碼執行過程中的儲存輸出內容的一個檔案描述符。

 

time:

介紹:包含關於時間的函數

常用函數:

import timeprint("--------時間戳記-------------")print("時間戳記time:",time.time())#時間戳記time: 1516435471.756463print("----------結構化時間(tm_year=2018, tm_mon=1.....-----------")print("struct_time:",time.gmtime(time.time()))#tm_year=2018, tm_mon=1.........print("timestamp->struct_time:",time.gmtime())#UTC時間print("local_time:",time.localtime())#本地時區時間print("struct_time->timstamp:",time.mktime(time.gmtime()))#結構化時間-->時間戳記print("----------ctime,asctime--------")print("string_time:",time.ctime())###字串時間 Mon Feb  5 01:02:06 2018print("asctime:",time.asctime())###字串時間 Mon Feb  5 01:02:06 2018print("----------format_time格式化時間、struct_time-----------")#結構化時間轉格式化時間:%Y代表year,%m代表month,%d代表day, %H代表hour,%M代表minute,%S代表second#只會取代%Y等字元,並不替換無對應意義的字元print("struct_time -> format_time:\n",  time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))y=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())#格式化時間轉結構化時間print("format_time -> struct_time:\n",time.strptime(y,"%Y-%m-%d %H:%M:%S"))print("------------year--------------")print("year:",time.localtime().tm_year)

 

 

random:

介紹:儲存著關於“隨機”的函數

常用函數:

import randomprint("---------0到1,隨機浮點值-----------")print(random.random())print("------------從範圍中,隨機取值,1<=x<=2--------")print(random.randint(1,2))print("------------從指定範圍中,隨機取值--------")print(random.randrange(1,3))print("------------從序列中,隨機值--------")print(random.choice("hello"))#從序列中隨機取值print(random.choice([0,11,3,99]))print("------------從序列中,隨機取指定個數值--------")print(random.sample('heigo',2))#print("------------隨機取浮點值,start,end--------")print(random.uniform(1,2))#start,endprint("-------洗牌,打亂排序-----")l=[0,3,4,5,67,9]random.shuffle(l)print(l)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.