Python文件、目錄函數

os和os.path模塊
os.listdir(dirname):列出dirname下的目錄和文件
os.getcwd():獲得當前工作目錄
os.curdir:返回但前目錄('.')
os.chdir(dirname):改變工作目錄到dirname
os.path.isdir(name):判斷name是不是一個目錄,name不是目錄就返回false
os.path.isfile(name):判斷name是不是一個文件,不存在name也返回false
os.path.exists(name):判斷是否存在文件或目錄name
os.path.getsize(name):獲得文件大小,如果name是目錄返回0L
os.path.abspath(name):獲得絕對路徑
os.path.normpath(path):規範path字符串形式
os.path.split(name):分割文件名與目錄(事實上,如果你完全使用目錄,它也會將最後一個目錄作爲文件名而分離,同時它不會判斷文件或目錄是否存在)
os.path.splitext():分離文件名與擴展名
os.path.join(path,name):連接目錄與文件名或目錄
os.path.basename(path):返回文件名
os.path.dirname(path):返回文件路徑
               
               
                    def __scanDir( self, dir ):
        for root, dirs, files in os.walk( dir, topdown=True):
            for name in files:
                path = os.path.join(root, name);
                if name == RmLogFile.LOG_FILE_NAME and os.path.isfile( path ):
                    
                    os.remove( path )
                    print "Find a file, remove it:", path
            for name in dirs:
                if name not in RmLogFile.EXCLUDE_PATHS:
                    self.__scanDir( name )

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章