os模組:
os.path.exists(path) -->bool #(path)檔案或者目錄存在os.path.abspath(path) -->str #(path)返迴文件或者目錄的絕對路徑# 下面幾個顧名思其義吧os.path.isfile(path) -->boolos.path.isdir(path) -->boolos.path.islink(path) -->boolos.path.join(p_parent,p_sub) -->str #返回新的path,父路徑+新路徑os.path.relpath(path[, start]) -->str #返回相對路徑os.sep -->返回系統的分隔字元==java的File.seperatoros.path.getsize(path) -->long 返迴文件的size#返回一個三元元組os.walk(dpath)(目錄abspath,[子目錄list],[子檔案list])
readfile.py:
#/bin/pythonimport osfor p in os.walk("D:\prj\py\part11_file\pfiles"):print p
目錄結構:D:\prj\py\part11_file
('D:\\prj\\py\\part11_file\\pfiles', ['dir1', 'dir2'], ['log.log', 'readfile.py', 'sometext.txt', 'stories.txt'])('D:\\prj\\py\\part11_file\\pfiles\\dir1', ['dir_par'], ['a.txt'])('D:\\prj\\py\\part11_file\\pfiles\\dir1\\dir_par', [], ['log.log'])('D:\\prj\\py\\part11_file\\pfiles\\dir2', [], ['readfile.py', 'stories.txt'])