清理空文件夹和空文件 param path: 文件路径,检查此文件路径下的子文件
(path)
| 81 | |
| 82 | |
| 83 | def delDir(path): |
| 84 | """ |
| 85 | 清理空文件夹和空文件 |
| 86 | param path: 文件路径,检查此文件路径下的子文件 |
| 87 | """ |
| 88 | try: |
| 89 | files = os.listdir(path) # 获取路径下的子文件(夹)列表 |
| 90 | for file in files: |
| 91 | if os.path.isdir(os.fspath(path+'/'+file)): # 如果是文件夹 |
| 92 | if not os.listdir(os.fspath(path+'/'+file)): # 如果子文件为空 |
| 93 | os.rmdir(os.fspath(path+'/'+file)) # 删除这个空文件夹 |
| 94 | else: |
| 95 | delDir(os.fspath(path+'/'+file)) # 遍历子文件 |
| 96 | if not os.listdir(os.fspath(path+'/'+file)): # 如果子文件为空 |
| 97 | os.rmdir(os.fspath(path+'/'+file)) # 删除这个空文件夹 |
| 98 | elif os.path.isfile(os.fspath(path+'/'+file)): # 如果是文件 |
| 99 | if os.path.getsize(os.fspath(path+'/'+file)) == 0: # 文件大小为0 |
| 100 | os.remove(os.fspath(path+'/'+file)) # 删除这个文件 |
| 101 | return |
| 102 | except FileNotFoundError: |
| 103 | print("文件夹路径错误") |
| 104 | |
| 105 | |
| 106 | def getFileMd5(file_name): |