Python_回調函數
阿新 • • 發佈:2017-06-20
exce 嘗試 fun logs 刪除文件夾 指定 spa 失敗 mdi
1 import os 2 import stat 3 4 def remove_readonly(func,path): #定義回調函數 5 os.chmod(path,stat.S_IWRITE) #刪除文件的只讀文件 6 func(path) #在次調用剛剛失敗的函數 7 8 def del_dir(path,onerror=None): #path為路徑 9 for file in os.listdir(path): 10 file_or_dir = os.path.join(path,file) 11 ifos.path.isdir(file_or_dir) and not os.path.islink(file_or_dir): 12 del_dir(file_or_dir) #遞歸刪除只文件夾及其文件 13 else: 14 try: 15 os.remove(file_or_dir) #嘗試刪除該文件 16 except: 17 if onerror and callable(onerror): 18 onerror(os.remove,file_or_dir) #自動調用回調函數 19 else: 20 print(‘You have an exception but did not capture it.‘) 21 os.rmdir(path) #刪除文件夾 22 23 del_dir(‘/Users/c2apple/Desktop/未命名文件夾 3‘,remove_readonly) #調用函數,指定回調函數
Python_回調函數