python模組 os模組的用法

2022-04-09 06:39:50 字數 1958 閱讀 9575

1

os.getcwd() 獲取當前工作的目錄,即當前python指令碼工作的目錄路徑

23 os.phdir("

dirname

") 改變當前指令碼的工作目錄;相當於shell下cd

45 os.curdir 返回當前目錄:('.'

)67 os.pardir 獲取當前目錄字串名:('..'

)89 os.makedirs('

dirname1/dirname2

') 可生成多層遞迴目錄

1011 os.removedirs('

dirname1

') 若目錄為空,則刪除;並遞迴到上一級目錄,如若也為空,則刪除,

12以此類推

1314 os.mkdir('

dirname

') 生成單機目錄;相當於shell種mkdir dirname

1516 os.rmdir('

dirname

') 刪除單級空目錄,若目錄不為空,則無法刪除,報錯;相當於

17shell種rmdir dirname

1819 os.listdir('

dirname

') 列出指定目錄下的所有檔案和子檔案目錄,包括隱藏檔案,並以列表方式列印

2021

os.remove() 刪除乙個檔案

2223 os.rename("

oldname

","newname

") 重新命名檔案/目錄

2425 os.stat('

path/filename

') 獲取檔案/目錄資訊

2627 os.sep 輸出作業系統特定的路徑分隔符,win下為"

\\" ,linux下為"\n"

2829 os.linesep 輸出用於分割檔案路徑的字串 win下為 '

;',linux下為':'

3031 os.name 輸出字串知識當前使用平台。win -> '

nt';linux -> '

posix'32

33 os.system("

bash command

") 執行shell命令,直接顯示

3435

os.environ 獲取系統環境變數

3637

os.path.abspath(path) 返回path規範化的絕對路徑

3839

os.path.split(path) 將path分割成目錄和檔名二元組返回

4041

os.path.dirname(path) 返回path的目錄。其實就是os.path.split (path)的第乙個元素

4243 os.path.basename(path) 返回path最後的檔名。如何path以/或\結尾,那麼就會返回空值,即

44os.path.split(path)的第二個元素

4546

os.path.exists(path) 如果path存在,返回true;如果path不存在,返回false

4748

os.path.isfile(path) 如果path是乙個存在的檔案,返回t入俄,否則返回false

4950

os.path.isabs(path) 如果是絕對路徑,返回true

5152

os.path.isdir(path) 如果path是乙個存在的目錄,返回true,否則返回false

5354

os.path.join(path1[,path2[,......]]) 將多個路徑組合後返回,第乙個絕對路徑之前的引數

55將被忽略

5657

os.path.getatime(path) 返回path所只想的檔案或目錄的最後訪問時間

5859 os.path.getmtime(path) 返回所指向的檔案或者目錄的最後修改時間

python模組 OS模組

bin env python coding utf 8 import os print os.name 輸出主機平台 print os.getcwd 輸出當前目錄 print os.listdir os.getcwd 輸出當前目錄的檔案 橫向 for i in os.listdir os.getcw...

python 模組 OS模組

print os.getcwd 輸出 e python workspace 原來 print os.getcwd 輸出 e python workspace 返回上級目錄 os.chdir os.getcwd 輸出 e python 更改 os.chdir r e print os.getcwd 輸...

Python 模組學習 os模組

一 os模組概述 python os模組包含普遍的作業系統功能。如果你希望你的程式能夠與平台無關的話,這個模組是尤為重要的。一語中的 二 常用方法 1 os.name 輸出字串指示正在使用的平台。如果是window 則用 nt 表示,對於linux unix使用者,它是 posix 2 os.get...