python中模块sys与os的一些常用方法
sys模塊提供了訪問或操作與python解釋器相關(guān)方法與對象。
我們就列舉出常用到的知識,以后,隨著學(xué)習(xí),不斷補(bǔ)充。
幾個(gè)常用到的動(dòng)態(tài)對象:
sys.argv,這是一個(gè)列表,它包含了所有傳遞給腳本的命令行參數(shù),其中第一個(gè)為腳本自身的名稱呀;
sys.path 這也是一個(gè)列表,里面放了模塊的搜索路經(jīng)。并且呢,path[0]表示當(dāng)腳本的路經(jīng)。
sys.modules, 這是一個(gè)字典類型,它里面放了所有載入的模塊。
sys.stdin , 標(biāo)準(zhǔn)輸入流--一個(gè)類文件對象, raw_input()與input()這是使用它。
sys.stdout,標(biāo)準(zhǔn)輸出流--一個(gè)類文件對象,我們使用print的時(shí)候,就是在使用它;
sys.stderr:標(biāo)準(zhǔn)錯(cuò)誤流--一個(gè)類文件對象?
(以上三個(gè)流對象都可以重定向到其它 的IO設(shè)備的。)
?
幾個(gè)常用的靜態(tài)對象:
sys.builtin_module_names: 這是一個(gè)元組,里面放的是python解釋器的內(nèi)置的所有模塊名稱。
sys.version ,存放了python解釋器的版本信息。
sys.platform , 存放的是python解釋器的平臺,返回linux2或windows
?
常用到的函數(shù):
exit([status]),退出,默認(rèn)為0,表示正常退出。
?
對于os模塊來說,首說一個(gè)通過os來調(diào)用的模塊:os.path,它存在于os模塊中,本身又是一個(gè)模塊。os.path只是一個(gè)別名而已,對于不同的平臺,這個(gè)模塊可能有不同的名字。如,posix式的系統(tǒng)(如unix,linux)的話,它的別名就叫做os.path.對于Mac、windows等來說,可能名字叫做macpach, ntpath。
模塊os.path下常見的函數(shù):
?
abspath(path):??? Return an absolute path,返回絕對路經(jīng)
basename(p):???? Returns the final component of a pathname:返回路經(jīng)的最后一部分,即 最后一個(gè) / 后的內(nèi)容;
commonprefix(list):??? Given a list of pathnames, returns the longest common leading componen,它的輸入為一個(gè)路經(jīng)
??????????????????????????? 的列表,用于返回list中,所有path共有的最長的路徑,從左向右,相同字符。
dirname(p):?? Returns the directory component of a pathname,返回目錄哦;
exists(path):??? Test whether a path exists.? Returns False for broken symbolic links,測試一個(gè)目錄是否存在;
expanduser(path):??? Expand ~ and ~user constructions.? If user or $HOME is unknown,do nothing.作用就是把目錄
?????????????????????????? 中的~展開;
getatime(filename):? 獲得最后一次訪問文件的時(shí)間,可以通過 os.stat()函數(shù)查看具體的狀態(tài);
getctime(filename):?? 返回元數(shù)據(jù)最后一次change的時(shí)間;
getmtime(filename):? 返回最后一次修改的時(shí)間;
isabs(s):????? 測試一個(gè)路經(jīng)是否是絕對路經(jīng);
isdir(s) :??????? Return true if the pathname refers to an existing directory.
isfile(path):??? Test whether a path is a regular file
join(path1[, path2[, ...]]); 將多個(gè)路徑組合后返回,第一個(gè)絕對路徑之前的參數(shù)將被忽略normcase(path): 在Linux下,該函數(shù)會(huì)原樣返回path,在windows平臺上會(huì)將路徑中所有字符轉(zhuǎn)換為小寫,并將所有斜杠轉(zhuǎn)換 為反斜杠 splitdrive(path): 拆分驅(qū)動(dòng)器名和路徑,主要對win,對linux元組第一個(gè)總是空的 splitext(path): 分離文件名與擴(kuò)展名;默認(rèn)返回(fname,fextension)元組,可做分片操作 ,以“.”為分隔符 getsize(path): 返回path的大小(字節(jié))?
在os模塊中,相關(guān)的函數(shù)太多了,只說幾個(gè)有用的,如果想具體看,可以通過 help(‘os’)查看哦;
? 一些與平臺相關(guān)的一些常量,平臺一一樣,返回的值也不一樣;???? - os.name is 'posix', 'nt', 'os2', 'ce' or 'riscos' ,操作系統(tǒng)的名字;
???? - os.curdir is a string representing the current directory ('.' or ':') 當(dāng)前目錄的表示;
???? - os.pardir is a string representing the parent directory ('..' or '::') 父目錄的表示;
???? - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')目錄名的分隔號;
???? - os.extsep is the extension separator ('.' or '/') 名字與擴(kuò)展名之間的分隔號;
???? - os.altsep is the alternate pathname separator (None or '/')
???? - os.pathsep is the component separator used in $PATH etc 目錄之間的分隔號,linux下為:。
???? - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')一行的分隔號;
???? - os.defpath is the default search path for executables,執(zhí)行程序時(shí)的默認(rèn)路經(jīng);linux下通常為bash的路經(jīng)
???? - os.devnull is the file path of the null device ('/dev/null', etc.) 空設(shè)備的文件路經(jīng);
?? ? 一些常用函數(shù): os.getcwd() 獲取當(dāng)前工作目錄,即當(dāng)前python腳本工作的目錄路徑 os.chdir("dirname") 改變當(dāng)前腳本工作目錄;相當(dāng)于shell下cd os.pardir 獲取當(dāng)前目錄的父目錄字符串名:('..') os.makedirs('dirname1/dirname2') 可生成多層遞歸目錄 os.removedirs('dirname1') 若目錄為空,則刪除,并遞歸到上一級目錄,如若也為空,則刪除,依此類推 os.mkdir('dirname') 生成單級目錄;相當(dāng)于shell中mkdir dirname os.rmdir('dirname') 刪除單級空目錄,若目錄不為空則無法刪除,報(bào)錯(cuò);相當(dāng)于shell中rmdir dirname os.listdir('dirname') 列出指定目錄下的所有文件和子目錄,包括隱藏文件,并以列表方式打印 os.remove() 刪除一個(gè)文件 os.rename("oldname","newname") 重命名文件/目錄 os.stat('path/filename') 獲取文件/目錄信息 os.symlink('path/filename','ln_filename') 創(chuàng)建符號鏈接,源需絕對路徑 os.utime() 修改時(shí)間屬性 os.system(command) 函數(shù)用來運(yùn)行shell命令:
?
總結(jié)
以上是生活随笔為你收集整理的python中模块sys与os的一些常用方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQLServer文件收缩-图形化+命令
- 下一篇: 《Python高效开发实战》实战演练——