本文實例講述了Python常用模塊sys,os,time,random功能與用法。分享給大家供大家參考,具體如下:
sys:
介紹:主要包含涉及python編譯器與系統交互的函數。
常用函數:
1
2
3
4
5
6
7
|
import sys print (sys.argv) #本文件名,已經運行該程序時的參數 #[如在命令窗口中python3 mysys.py 參數1 參數2] #那么參數1為sys.argv[1],以此類推 print (sys.version) #python版本號 print (sys.path) #返回模塊的搜索路徑,初始化時使用PYTHONPATH環境變量的值 # sys.exit(0)#中途退出程序,當參數非0時,會引發一個SystemExit異常 |
1
2
|
sys.stdout.write() #在屏幕中打印 sys.stdout.flush() #刷新標準緩沖區 |
os:
介紹:這個模塊提供了一種方便的使用操作系統函數的方法。
常用函數:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
import os print ( "-------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:c") #要創建c,如果a不存在則創建a,如果b不存在則創建b print ( "-----remodir遞歸刪除目錄---------" ) #os.removedirs(r"c: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)#nt print ( "-------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 '.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的主要區別是前者返回值是腳本的退出狀態碼,后者的返回值是腳本執行過程中的存儲輸出內容的一個文件描述符。
附:
subprocess模塊是python從2.4版本開始引入的模塊。主要用來取代 一些舊的模塊方法,如os.system、os.spawn*、os.popen*、commands.*等。subprocess通過子進程來執行外部指令,并通過input/output/error管道,獲取子進程的執行的返回信息。
time:
介紹:包含關于時間的函數
常用函數:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import time print ( "--------時間戳-------------" ) print ( "時間戳time:" ,time.time()) #時間戳time: 1516435471.756463 print ( "----------結構化時間(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 2018 print ( "asctime:" ,time.asctime()) ###字符串時間 Mon Feb 5 01:02:06 2018 print ( "----------format_time格式化時間、struct_time-----------" ) #結構化時間轉格式化時間:%Y代表year,%m代表month,%d代表day, %H代表hour,%M代表minute,%S代表second #只會取代%Y等字符,并不替換無對應意義的字符 print ( "struct_time -> format_time: " , 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: " ,time.strptime(y, "%Y-%m-%d %H:%M:%S" )) print ( "------------year--------------" ) print ( "year:" ,time.localtime().tm_year) |
random:
介紹:存儲著關于“隨機”的函數
常用函數:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import random print ( "---------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,end print ( "-------洗牌,打亂排序-----" ) l = [ 0 , 3 , 4 , 5 , 67 , 9 ] random.shuffle(l) print (l) |
希望本文所述對大家Python程序設計有所幫助。
原文鏈接:https://www.cnblogs.com/progor/p/8415476.html