python之sys與os模組詳解

來源:互聯網
上載者:User

標籤:list   組合   ring   dir   變數   sys   報錯   字串   sdi   

sys模組:

sys.argv        命令列參數List,第一個元素是程式本身路徑

sys.exit(n)      退出程式,正常退出時exit(0)

sys.version      擷取Python解釋程式的版本資訊 sys.maxint       最大的 Int sys.path        返回模組的搜尋路徑,初始化時使用PYTHONPATH環境變數的值 sys.platform     返回作業系統平台名稱 sys.stdin       輸入相關 sys.stdout       輸出相關 sys.stderror     錯誤相關 os模組: os.getcwd()                 擷取當前工作目錄,即當前python指令碼工作的目錄路徑 os.chdir( "dirname" )         改變當前指令碼工作目錄;相當於shell下cd os.curdir                  返回目前的目錄: ( ‘.‘ ) os.pardir                  擷取目前的目錄的父目錄字元串名:( ‘..‘ ) os.makedirs( ‘dir1/dir2‘ )        可產生多層遞迴目錄 os.removedirs( ‘dirname1‘ )       若目錄為空白,則刪除,並遞迴到上一級目錄,如若也為空白,則刪除,依此類推 os.mkdir( ‘dirname‘ )         產生單級目錄;相當於shell中mkdir dirname os.rmdir( ‘dirname‘ )         刪除單級空目錄,若目錄不為空白則無法刪除,報錯;相當於shell中rmdir dirname os.listdir( ‘dirname‘ )          列出指定目錄下的所有檔案和子目錄,包括隱藏檔案,並以列表方式列印 os.remove()                 刪除一個檔案 os.rename( "oldname" , "new" )       重新命名檔案 / 目錄 os.stat( ‘path/filename‘ )        擷取檔案 / 目錄資訊 os.sep                    作業系統特定的路徑分隔字元,win下為 "\\",Linux下為" / " os.linesep             當前平台使用的行終止符,win下為 "\t\n" ,Linux下為 "\n" os.pathsep             用於分割檔案路徑的字串 os.name               字串指示當前使用平台。win - > ‘nt‘ ; Linux - > ‘posix‘ os.system( "bash command" )       運行shell命令,直接顯示 os.environ             擷取系統內容變數 os.path.abspath(path)          返回path正常化的絕對路徑 os.path.split(path)         將path分割成目錄和檔案名稱二元組返回 os.path.dirname(path)          返回path的目錄。其實就是os.path.split(path)的第一個元素 os.path.basename(path)         返回path最後的檔案名稱。如何path以/或\結尾,那麼就會返回空值。即os.path.split(path)的第二個元素 os.path.exists(path)           如果path存在,返回 True ;如果path不存在,返回 False os.path.isabs(path)           如果path是絕對路徑,返回 True os.path.isfile(path)           如果path是一個存在的檔案,返回 True 。否則返回 False os.path.isdir(path)         如果path是一個存在的目錄,則返回 True 。否則返回 False os.path.join(path1[, path2[, ...]]) 將多個路徑組合後返回,第一個絕對路徑之前的參數將被忽略 os.path.getatime(path)         返回path所指向的檔案或者目錄的最後存取時間 os.path.getmtime(path)         返回path所指向的檔案或者目錄的最後修改時間

python之sys與os模組詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.