# -*- coding:utf-8 -*- #os模塊 import os #輸出字符串指示正在使用的平臺。若是是window 則用'nt'表示,對於Linux/Unix用戶,它是'posix'。 print os.name #獲取當前目錄 print os.getcwd() #返回指定目錄下的全部文件和目錄名。 print os.listdir(os.getcwd()) #刪除文件 os.remove('/opt/case/a.txt') #運行shell命令。 os.system('cmd') #返回一個路徑的目錄名和文件名 print os.path.split('/opt/case/a.txt') #函數用來檢驗給出的路徑是否真地存在 print os.path.exists(os.getcwd()) #得到絕對路徑 print os.path.abspath('../') #得到文件大小,若是name是目錄返回0L print os.path.getsize('D:/Python27/README.txt') #分離文件名與擴展名=> ('README', '.txt') print os.path.splitext('README.txt') #返回文件名=>README.txt print os.path.basename('/opt/case/README.txt') #:返回文件路徑=>/opt/case print os.path.dirname('/opt/case/README.txt') #建立目錄 os.mkdir('./a') #刪除目錄 os.rmdir('./a')