您好,登錄后才能下訂單哦!
這篇文章主要介紹了Python os模塊常用方法和屬性總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1. os 模塊常用的方法及屬性
os.sep:取代操作系統(tǒng)特定的路徑分隔符 os.name:指示你正在使用的工作平臺(tái)。比如對(duì)于Windows,它是'nt',而對(duì)于Linux/Unix用戶,它是'posix'。 os.getcwd:得到當(dāng)前工作目錄,即當(dāng)前python腳本工作的目錄路徑。 os.getenv()和os.putenv:分別用來讀取和設(shè)置環(huán)境變量 os.listdir():返回指定目錄下的所有文件和目錄名 os.remove(file):刪除一個(gè)文件 os.stat(file): 獲得文件屬性 os.chmod(file): 修改文件權(quán)限和時(shí)間戳 os.mkdir(name): 創(chuàng)建目錄 os.rmdir(name): 刪除目錄 os.removedirs(r“c:\python”): 刪除多個(gè)目錄 os.system(): 運(yùn)行shell命令。os.system 的結(jié)果只是命令執(zhí)行結(jié)果的返回值,執(zhí)行成功為0 os.popen(command[, mode[, bufsize]]): os.popen() 方法用于從一個(gè)命令打開一個(gè)管道。popen返回的是file read的對(duì)象,對(duì)其進(jìn)行讀取使用read() os.exit(): 終止當(dāng)前進(jìn)程 os.mknod(): 創(chuàng)建空文件 os.linesep: 給出當(dāng)前平臺(tái)的行終止符。例如,Windows使用'\r\n',Linux使用'\n'而Mac使用'\r' os.path.exist(): 檢驗(yàn)給出的路徑是否真的存在 os.listdir(dirname): 列出dirname下的目錄和文件 os.getcwd(): 獲得當(dāng)前工作目錄 os.curdir: 返回當(dāng)前目錄('.') os.chdir(dirname): 改變工作目錄到dirname os.path.isdir(name): 判斷name是不是目錄,不是目錄就返回false os.path.isfile(name): 判斷name這個(gè)文件是否存在,不存在返回false os.path.exists(name): 判斷是否存在文件或目錄name os.path.getsize(name): 或得文件大小,如果name是目錄返回0L os.path.getatime(path): 返回上次訪問路徑的時(shí)間,返回值是一個(gè)浮點(diǎn)數(shù) os.path.getmtime(path): 返回上次修改一路徑的時(shí)間,返回值是一個(gè)浮點(diǎn)數(shù) os.path.getctime(path): 返回系統(tǒng)的ctime,在某些系統(tǒng)(如Unix)上是最后一次元數(shù)據(jù)更改的時(shí)間,而在其他系統(tǒng)(如Windows)上則是路徑的創(chuàng)建時(shí)間。返回值是一個(gè)數(shù)字 os.path.abspath(name): 返回一個(gè)目錄的絕對(duì)路徑 os.path.realpath(path): 返回指定文件的標(biāo)準(zhǔn)路徑,而非軟鏈接所在的路徑 os.path.isabs(): 判斷是否為絕對(duì)路徑 os.path.normpath(path): 規(guī)范path字符串形式。示例: os.path.normpath('c://windows\\System32\\../Temp/') # 輸出'c:\\windows\\Temp' os.path.split(name): 分割文件名與目錄(事實(shí)上,如果你完全使用目錄,它也會(huì)將最后一個(gè)目錄作為文件名而分離,同時(shí)它不會(huì)判斷文件或目錄是否存在),示例: ('F:/Technology-20161005/python/python_project/demo/os_sysdemo', 'sysdemo.py') os.path.splitext(): 分離文件名和擴(kuò)展名 # ('F:/Technology-20161005/python/python_project/demo/os_sysdemo/sysdemo', '.py') os.path.join(path,name): 連接目錄與文件名或目錄 os.path.basename(path): 返回文件名 os.path.dirname(path): 返回文件路徑
2. 常用方法
統(tǒng)計(jì)當(dāng)前目錄下已經(jīng)創(chuàng)建的文件
current_path = os.path.realpath('.') start_time = time.time() # 判斷當(dāng)前路徑下比當(dāng)前時(shí)刻早的已經(jīng)創(chuàng)建的文件 created_files = [name for name in os.listdir(current_path) if os.path.isfile(os.path.join(current_path, name)) and os.path.getctime(os.path.join(current_path, name)) < start_time] print created_files
統(tǒng)計(jì)當(dāng)前目錄下后創(chuàng)建的文件
current_path = os.path.realpath('.') start_time = time.time() # 判斷當(dāng)前路徑下后創(chuàng)建的文件 created_files = [name for name in os.listdir(current_path) if os.path.isfile(os.path.join(current_path, name)) and os.path.getmtime(os.path.join(current_path, name)) > start_time] print created_files
刪除文件操作
import os, errno def silent_remove_of_file(file): try: os.remove(file) except OSError as e: if e.errno != errno.ENOENT: raise e return False return True
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。