資料夾,又稱目錄。關於目錄操作的函式、物件等主要集中在三個標準庫:os、os.path、shutil。
一、os 模組 ———— 基本操作
1、建立新目錄
(1)os.mkdir(r』路徑/目錄名』)
建立指定的目錄,無返回值,建立不成功則丟擲異常。為了避免斜槓與反斜槓引起的麻煩,'路徑/目錄名』應該用原始字串形式,即在前面加上r 或 r。例如:
import os
os.mkdir(r'c:/users/administrator/desktop/測試目錄'
)# 建立目錄:測試目錄
os.mkdir(r'c:/users/administrator/desktop/測試目錄'
)# fileexistserror: 已存在
(2)os.makedirs(r』路徑/目錄名』)
與 os.mkdir() 類似,也是建立目錄,無返回值,但如果路徑中的目錄如果還不存在,則一併建立。例如:
import os
os.mkdir(r'c:/users/administrator/desktop/測試目錄/aa/bb/cc/dd'
)# filenotfounderror:找不到指定的路徑
os.makedirs(r'c:/users/administrator/desktop/測試目錄/aa/bb/cc/dd'
)# 建立目錄:aa、bb、cc、dd
2、重新命名和移動目錄
os.rename(r』路徑/舊目錄名』,r』路徑/新目錄名』)
這個函式兼有重新命名和移動的功能,無返回值,重新命名或移動不成功則丟擲異常。當前後路徑一致時,進行「重新命名」;當前後路徑不一致時,進行「移動」。例如:
import os
os.replace(r'c:/users/administrator/desktop/測試目錄/aa/bb/cc/dd'
,r'c:/users/administrator/desktop/測試目錄/aa/bb/cc/newdd'
)# dd 目錄重新命名為:newdd
os.replace(r'c:/users/administrator/desktop/測試目錄/aa/bb/cc'
,r'c:/users/administrator/desktop/測試目錄/aa/newcc'
)# cc 目錄被移動,且重新命名為:newcc
3、刪除目錄
os.rmdir(r』路徑/目錄名』)
刪除指定的目錄,無返回值,刪除不成功則丟擲異常。只能刪除空目錄,其中有子目錄或檔案的目錄不能被刪除。例如:
import os
os.rmdir(r'c:/users/administrator/desktop/測試目錄/aa/newcc/newdd'
)# newdd 目錄被刪除
os.rmdir(r'c:/users/administrator/desktop/測試目錄/aa'
)# oserror:[winerror 145] 目錄不是空的
4、取目錄中的所有內容
os.listdir(r』路徑/目錄名』)
返回乙個列表,該列表包含了目錄中的所有檔案與目錄的名稱。例如:
import os
print
(os.listdir(r'c:/users/administrator/desktop/測試目錄/aa'))
# ['aa-檔案一.txt', 'aa-檔案三.txt', 'aa-檔案二.txt', 'aa-檔案四.txt', 'bb', 'newcc']
5、取目錄及其子目錄中的所有內容
os.walk(r』路徑/目錄名』)
取目錄、子目錄、子子目錄…中的所有內容,返回乙個生成器物件。返回的生成器物件的元素為元組。
乙個目錄或子目錄構成乙個元組:(路徑/目錄,[子目錄列表],[檔案列表])。例如:
import os.path
t = os.walk(r'c:/users/administrator/desktop/測試目錄'
)print
(t)#
print
([x for x in t]
)# [('c:/users/administrator/desktop/測試目錄',
# ['aa'],
# ),
# ('c:/users/administrator/desktop/測試目錄\\aa',
# ['bb', 'newcc'],
# ['aa-檔案一.txt', 'aa-檔案三.txt', 'aa-檔案二.txt', 'aa-檔案四.txt']),
# ('c:/users/administrator/desktop/測試目錄\\aa\\bb',
# ,
# ['bb-檔案一.txt', 'bb-檔案三.txt', 'bb-檔案二.txt', 'bb-檔案四.txt']),
# ('c:/users/administrator/desktop/測試目錄\\aa\\newcc',
# ['dd'],
# ['cc-檔案一.txt', 'cc-檔案三.txt', 'cc-檔案二.txt', 'cc-檔案四.txt']),
# ('c:/users/administrator/desktop/測試目錄\\aa\\newcc\\dd',
# ,
# ['dd-檔案一.txt', 'dd-檔案二.txt'])]
二、os.path 模組 ———— 路徑操作
1、分隔路徑和目錄
os.path.split(r』路徑/目錄名』),返回組成的元組:(路徑,目錄名)。
os.path.dirname((r』路徑/目錄名』),返回路徑。
os.path.basename((r』路徑/目錄名』),返回目錄名。
例如:
import os.path
print
(os.path.split(r'c:/users/administrator/desktop/測試目錄'))
# ('c:/users/administrator/desktop', '測試目錄')
print
(os.path.dirname(r'c:/users/administrator/desktop/測試目錄'))
# c:/users/administrator/desktop
print
(os.path.basename(r'c:/users/administrator/desktop/測試目錄'))
# 測試目錄
2、判斷是否目錄
os.path.isdir(r』路徑』),是目錄則返回true,不是目錄或目錄不存在返回false。例如:
import os.path
print
(os.path.isdir(r'c:/users/administrator/desktop/測試目錄'))
# true
print
(os.path.isdir(r'c:/users/administrator/desktop/測試目錄aaa'))
# false
3、連線多個路徑
os.path.join(r』路徑1』, r』路徑2』, r』路徑3』…),返回連線後的路徑。例如:
import os.path
print
(os.path.join(r'c:/users'
,r'administrator'
,r'desktop/測試目錄'))
# c:/users\administrator\desktop/測試目錄
三、shutil 模組(shell utility 的縮寫,外殼實用程式) ———— 高階操作
1、遞迴複製目錄
copytree(r』路徑/舊目錄名』,r』路徑/新目錄名』)
複製目錄,返回「路徑/新目錄名」。例如:
import shutil
print
(shutil.copytree(r'c:/users/administrator/desktop/測試目錄/aa/bb/cc'
,r'c:/users/administrator/desktop/測試目錄/aa/cc'))
# aa 中新增複製來的目錄 cc :c:/users/administrator/desktop/測試目錄/aa/cc
2、遞迴刪除目錄
rmtree(r』路徑/目錄名』)
刪除指定的目錄,無返回值。
import shutil
shutil.rmtree(r'c:/users/administrator/desktop/測試目錄/aa/cc'
)# cc 目錄及其目錄中的內容被刪除
13 資料夾和目錄操作
2019獨角獸企業重金招聘python工程師標準 獲取當前目錄 getcwd char cwd 300 getcwd cwd,sizeof cwd 設定當前目錄 chdir chdir home 建立目錄 mkdir mkdir test 0751 刪除目錄 rmdir,remove rmdir t...
UNIXC001 資料夾操作
drwxrwxr x 2 moonx moonx 4096 12月 26 09 12 test 資料夾裡x代表可通過的意思 chmod a x test cd test bash cd test permission denied chmod a x test cd test touch filea...
No000083 檔案與資料夾操作
region folder option 資料夾操作 指定目錄是否存在 資料夾路徑 public static bool direxist string dirpath else catch exception ex 建立目錄 public static void makedir string di...