os.sep:取代操作系統特定的路徑分隔符 os.name:指示你正在使用的工作平台。比如對於Windows,它是'nt',而對於Linux/Unix用戶,它是'posix'。 os.getcwd:得到當前工作目錄,即當前python腳本工作的目錄路徑。 os.getenv()和os.putenv:分別用來讀取和設置環境變量 os.listdir():返回指定目錄下的所有文件和目錄名 os.remove(file):刪除一個文件 os.stat(file):獲得文件屬性 os.chmod(file):修改文件權限和時間戳 os.mkdir(name):創建目錄 os.rmdir(name):刪除目錄 os.removedirs(r“c:\python”):刪除多個目錄 os.system():運行shell命令 os.exit():終止當前進程 os.linesep:給出當前平台的行終止符。例如,Windows使用'\r\n',Linux使用'\n'而Mac使用'\r' os.path.split():返回一個路徑的目錄名和文件名 os.path.isfile()和os.path.isdir()分別檢驗給出的路徑是一個目錄還是文件 os.path.existe():檢驗給出的路徑是否真的存在 os.listdir(dirname):列出dirname下的目錄和文件 os.getcwd():獲得當前工作目錄 os.curdir:返回當前目錄('.') os.chdir(dirname):改變工作目錄到dirname os.path.isdir(name):判斷name是不是目錄,不是目錄就返回false os.path.isfile(name):判斷name這個文件是否存在,不存在返回false os.path.exists(name):判斷是否存在文件或目錄name os.path.getsize(name):或得文件大小,如果name是目錄返回0L os.path.abspath(name):獲得絕對路徑 os.path.isabs():判斷是否為絕對路徑 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):返回文件路徑
目錄操作
os.mkdir("file") 創建目錄 shutil.copyfile("oldfile","newfile") 復制文件:oldfile和newfile都只能是文件 shutil.copy("oldfile","newfile") oldfile只能是文件夾,newfile可以是文件,也可以是目標目錄 shutil.copytree("olddir","newdir") 復制文件夾.olddir和newdir都只能是目錄,且newdir必須不存在 os.rename("oldname","newname") 重命名文件(目錄).文件或目錄都是使用這條命令 shutil.move("oldpos","newpos") 移動文件(目錄) os.rmdir("dir") 只能刪除空目錄 shutil.rmtree("dir") 空目錄、有內容的目錄都可以刪 os.chdir("path") 轉換目錄,換路徑