標籤:lines 定義 tde now() 命名 logging path環境變數 ptime ogg
# 內建模組,第三方模組,自訂模組# 匯入模組根據那個路徑作為基準?sys.path# 可以通過sys.path.append(‘‘)添加路徑; import sys import os project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(project_path)# 1.sys | python解譯器 sys.argv 命令列參數List,第一個元素是程式本身路徑 sys.exit(n) 退出程式,正常退出時exit(0) sys.version 擷取Python解釋程式的版本資訊 sys.maxint 最大的Int值 sys.path 返回模組的搜尋路徑,初始化時使用PYTHONPATH環境變數的值 sys.platform 返回作業系統平台名稱 sys.stdin 輸入相關 sys.stdout 輸出相關 sys.stderror 錯誤相關 # 2.os | 作業系統 os.getcwd() 擷取當前工作目錄,即當前python指令碼工作的目錄路徑 os.chdir("dirname") 改變當前指令碼工作目錄;相當於shell下cd os.curdir 返回目前的目錄: (‘.‘) os.pardir 擷取目前的目錄的父目錄字元串名:(‘..‘) os.makedirs(‘dir1/dir2‘) 可產生多層遞迴目錄 os.removedirs(‘dirname1‘) 若目錄為空白,則刪除,並遞迴到上一級目錄,如若也為空白,則刪除,依此類推 os.mkdir(‘dirname‘) 產生單級目錄;相當於shell中mkdir dirname os.rmdir(‘dirname‘) 刪除單級空目錄,若目錄不為空白則無法刪除,報錯;相當於shell中rmdir dirname os.listdir(‘dirname‘) 列出指定目錄下的所有檔案和子目錄,包括隱藏檔案,並以列表方式列印 os.remove() 刪除一個檔案 os.rename("oldname","new") 重新命名檔案/目錄 os.stat(‘path/filename‘) 擷取檔案/目錄資訊 os.sep 作業系統特定的路徑分隔字元,win下為"\\",Linux下為"/" os.linesep 當前平台使用的行終止符,win下為"\t\n",Linux下為"\n" os.pathsep 用於分割檔案路徑的字串 os.name 字串指示當前使用平台。win->‘nt‘; Linux->‘posix‘ os.system("bash command") 運行shell命令,直接顯示 os.environ 擷取系統內容變數 os.path.abspath(path) 返回path正常化的絕對路徑 os.path.split(path) 將path分割成目錄和檔案名稱二元組返回 os.path.dirname(path) 返回path的目錄。其實就是os.path.split(path)的第一個元素 os.path.basename(path) 返回path最後的檔案名稱。如何path以/或\結尾,那麼就會返回空值。即os.path.split(path)的第二個元素 os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False os.path.isabs(path) 如果path是絕對路徑,返回True os.path.isfile(path) 如果path是一個存在的檔案,返回True。否則返回False os.path.isdir(path) 如果path是一個存在的目錄,則返回True。否則返回False os.path.join(path1[, path2[, ...]]) 將多個路徑組合後返回,第一個絕對路徑之前的參數將被忽略 os.path.getatime(path) 返回path所指向的檔案或者目錄的最後存取時間 os.path.getmtime(path) 返回path所指向的檔案或者目錄的最後修改時間# 3.hashlib# 代替了md5模組和sha模組,# 主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 演算法 import hashlib # ######## md5 ######## hash = hashlib.md5() # help(hash.update) hash.update(bytes(‘admin‘, encoding=‘utf-8‘)) print(hash.hexdigest()) print(hash.digest()) ######## sha1 ######## hash = hashlib.sha1() hash.update(bytes(‘admin‘, encoding=‘utf-8‘)) print(hash.hexdigest()) # ######## sha256 ######## hash = hashlib.sha256() hash.update(bytes(‘admin‘, encoding=‘utf-8‘)) print(hash.hexdigest()) # ######## sha384 ######## hash = hashlib.sha384() hash.update(bytes(‘admin‘, encoding=‘utf-8‘)) print(hash.hexdigest()) # ######## sha512 ######## hash = hashlib.sha512() hash.update(bytes(‘admin‘, encoding=‘utf-8‘)) print(hash.hexdigest()) # 4.random import random print(random.random()) print(random.randint(1, 2)) print(random.randrange(1, 10)) # 隨機驗證碼 import random checkcode = ‘‘ for i in range(4): current = random.randrange(0,4) if current != i: temp = chr(random.randint(65,90)) else: temp = random.randint(0,9) checkcode += str(temp) print checkcode # 5.re# 6.json# 用於【字串】和 【python基礎資料型別 (Elementary Data Type)】 間進行轉換# 7.pickle# 用於【python特有的類型】 和 【python基礎資料型別 (Elementary Data Type)】間進行轉換# 8.logging# 用於便捷記錄日誌且安全執行緒的模組# 9.time# 時間戳記 1970年1月1日之後的秒,即:time.time()# 格式化的字串 2014-11-11 11:11, 即:time.strftime(‘%Y-%m-%d‘)# 結構化時間 元組包含了:年、日、星期等... time.struct_time 即:time.localtime() print time.time() print time.mktime(time.localtime()) print time.gmtime() #可加時間戳記參數 print time.localtime() #可加時間戳記參數 print time.strptime(‘2014-11-11‘, ‘%Y-%m-%d‘) print time.strftime(‘%Y-%m-%d‘) #預設目前時間 print time.strftime(‘%Y-%m-%d‘,time.localtime()) #預設目前時間 print time.asctime() print time.asctime(time.localtime()) print time.ctime(time.time()) import datetime ‘‘‘ datetime.date:表示日期的類。常用的屬性有year, month, day datetime.time:表示時間的類。常用的屬性有hour, minute, second, microsecond datetime.datetime:表示日期時間 datetime.timedelta:表示時間間隔,即兩個時間點之間的長度 timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]]) strftime("%Y-%m-%d") ‘‘‘ import datetime print datetime.datetime.now() print datetime.datetime.now() - datetime.timedelta(days=5)
python常用模組 | Python