首页 > 编程语言 > 详细

Python_回调函数

时间:2017-06-20 14:32:58      阅读:337      评论:0      收藏:0      [点我收藏+]
 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         if os.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_回调函数

原文:http://www.cnblogs.com/cmnz/p/7053873.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!