Python3.5——內建模組詳解之os模組、sys模組、shutil模組
阿新 • • 發佈:2019-01-01
1、os模組:提供對作業系統進行呼叫的介面
執行結果:#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu import os print(os.getcwd()) #獲取當前的操作目錄,即當前Python指令碼工作的目錄路徑 #os.chdir("F:\\PythonCode\\day5\\test") #改變當前指令碼工作目錄,相當於shell下的cd os.chdir(r"F:\PythonCode\day5\test") #與上面一句等價(推薦使用) print(os.getcwd()) print(os.curdir) #返回當前目錄 '.' print(os.pardir) #獲取當前目錄的父目錄字串名 '..' os.makedirs(r"F:\a\b\c") #生成多層遞迴目錄 #若目錄為空,則刪除,並遞迴到上一級目錄,如若也為空,則刪除,依此類推 os.removedirs(r"F:\a\b\c") #清理空資料夾 os.mkdir(r"F:\PythonCode\day5\t") #生成單級目錄,相當於shell中mkdir filename os.rmdir(r"F:\PythonCode\day5\t") #刪除單級空目錄,若目錄不為空,無法刪除或報錯;相當於shell中rmdir filename print(os.listdir(r"F:\PythonCode\day5\test")) #列出指定目錄下的所有檔案和子目錄,包括隱藏檔案,並以列表方式列印 os.remove(r"F:\PythonCode\day5\test\1.py") #刪除一個檔案 os.rename(r"F:\PythonCode\day5\test\1.py",r"F:\PythonCode\day5\test\2.py") #重新命名檔案/目錄 print(os.stat(r"F:\PythonCode\day5\test")) #獲取檔案/目錄資訊 print(os.sep) #輸出作業系統特定的路徑分隔符,win下為"\\",Linux下為"/" print(os.linesep) #輸出當前平臺使用的行終止符,win下為"\r\n",Linux下為"\n" print(os.pathsep) #輸出用於分割檔案路徑的字串,win下為";",Linux下為":" print(os.environ) #檢視系統的環境變數 print(os.name) #輸出字串指示當前使用平臺。win->'nt'; Linux->'posix' print(os.system("dir")) #執行shell命令,直接顯示 print(os.path.abspath(r"F:\PythonCode\day5")) #返回path規範化的絕對路徑 print(os.path.split(r"F:\PythonCode\day5\test\1.py")) #將path分割成目錄和檔名二元組返回 print(os.path.dirname(r"F:\PythonCode\day5")) #返回path的目錄。其實就是os.path.split(path)的第一個元素 print(os.path.basename(r"F:\PythonCode\day5")) #返回path最後的檔名。如何path以/或\結尾,那麼就會返回空值。 print(os.path.exists(r"F:\PythonCode\day5")) #如果path存在,返回True;如果path不存在,返回False print(os.path.isabs(r"F:\PythonCode\day5")) #如果path是絕對路徑,返回True print(os.path.isfile(r"F:\PythonCode\day5\p_test.py")) #如果path是一個存在的檔案,返回True,否則返回False print(os.path.isdir(r"F:\PythonCode\day5")) #如果path是一個存在的目錄,則返回True。否則返回False print(os.path.join(r"F:",r"\PythonCode",r"\day5",r"\day")) #將多個路徑組合後返回,第一個絕對路徑之前的引數將被忽略 print(os.path.getatime(r"F:\PythonCode\day5")) #返回path所指向的檔案或者目錄的最後存取時間 print(os.path.getmtime(r"F:\PythonCode\day5")) #返回path所指向的檔案或者目錄的最後修改時間
2、sys模組F:\PythonCode\day5 F:\PythonCode\day5\test . .. ['2.py', 'module_test.py', 'test.py'] os.stat_result(st_mode=16895, st_ino=8162774324625191, st_dev=104211, st_nlink=1, st_uid=0, st_gid=0, st_size=4096, st_atime=1506609480, st_mtime=1506609480, st_ctime=1506579769) \ ; environ({'PROCESSOR_LEVEL': '6', 'WINDOWS_TRACING_LOGFILE': 'C:\\BVTBin\\Tests\\installpackage\\csilogfile.log', 'PROCESSOR_ARCHITECTURE': 'x86') nt ������ F �еľ��� ѧϰ�� ������к��� 0001-9713 F:\PythonCode\day5\test ��Ŀ¼ 2017/09/28 22:38 <DIR> . 2017/09/28 22:38 <DIR> .. 2017/09/28 22:37 69 2.py 2017/09/28 14:31 121 module_test.py 2017/09/28 14:35 237 test.py 3 ���ļ� 427 �ֽ� 2 ��Ŀ¼ 14,051,733,504 �����ֽ� 0 F:\PythonCode\day5 ('F:\\PythonCode\\day5\\test', '1.py') F:\PythonCode day5 True True True True F:\day 1506656912.210523 1506656912.210523
執行結果:#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu import sys print(sys.argv) #命令列引數List,第一個元素是程式本身路徑 print(sys.version) #獲取Python解釋程式的版本資訊 print(sys.path) #返回模組的搜尋路徑,初始化時使用PYTHONPATH環境變數的值 print(sys.platform) #返回作業系統平臺名稱 sys.stdout.write('please:') #標準輸出,寫入字串輸出到螢幕 val = sys.stdin.readline()[:-1] #標準輸入 print(val) sys.exit(0) #退出程式,正常退出時exit(0)
['F:/PythonCode/day5/sys_module.py']
3.5.2 |Anaconda 4.2.0 (32-bit)| (default, Jul 5 2016, 11:45:57) [MSC v.1900 32 bit (Intel)]
['F:\\PythonCode\\day5', 'F:\\PythonCode', 'C:\\Users\\Administrator\\Anaconda3\\python35.zip', 'C:\\Users\\Administrator\\Anaconda3\\DLLs', 'C:\\Users\\Administrator\\Anaconda3\\lib', 'C:\\Users\\Administrator\\Anaconda3', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\Sphinx-1.4.6-py3.5.egg', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\win32', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\setuptools-27.2.0-py3.5.egg']
win32
hello
please:hello
3、shutil模組:高階的檔案、資料夾、壓縮包處理模組
(1)將檔案內容拷貝到另一個檔案中,可以部分內容——shutil.copyfileobj(fsrc, fdst[, length])
f1 = open("p_test.py",encoding="utf-8")
f2 = open("p1.py","w",encoding="utf-8")
#將檔案p_test.py內容拷貝到另一個檔案p1.py中,可以部分內容
shutil.copyfileobj(f1,f2)
執行結果:
(2)拷貝檔案——shutil.copyfile(src, dst)
import shutil
shutil.copyfile("p1.py","p2.py") #拷貝檔案
執行結果:
(3)僅拷貝許可權(內容、組、使用者均不變)——shutil.copymode(src, dst)
(4)拷貝狀態的資訊,包括:mode bits, atime, mtime, flags——shutil.copystat(src, dst)
(5)拷貝檔案和許可權——shutil.copy(src, dst)
(6)拷貝檔案和狀態資訊——shutil.copy2(src, dst)
(7)遞迴的去拷貝檔案:
shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
import shutil
shutil.copytree("test","test1") #遞迴的去拷貝檔案
執行結果:
(8)遞迴的去刪除檔案——shutil.rmtree(path[, ignore_errors[, onerror]])
(9)遞迴的去移動檔案——shutil.move(src, dst)
(10)建立壓縮包並返回檔案路徑,例如:zip、tar——shutil.make_archive(base_name, format,...)
- base_name: 壓縮包的檔名,也可以是壓縮包的路徑。只是檔名時,則儲存至當前目錄,否則儲存至指定路徑,
如:www =>儲存至當前路徑
如:/Users/wupeiqi/www =>儲存至/Users/wupeiqi/ - format: 壓縮包種類,“zip”, “tar”, “bztar”,“gztar”
- root_dir: 要壓縮的資料夾路徑(預設當前目錄)
- owner: 使用者,預設當前使用者
- group: 組,預設當前組
- logger: 用於記錄日誌,通常是logging.Logger物件
import shutil
shutil.make_archive("shutil_archive_test","zip","F:\PythonCode\day5")
執行結果:
總結:shutil 對壓縮包的處理是呼叫 ZipFile 和 TarFile 兩個模組來進行的
import zipfile
z = zipfile.ZipFile("day5.zip","w")
z.write("p1.py")
print("===========")
z.write("p2.py")
z.close()
執行結果:
===========