【发布时间】:2018-07-18 10:06:06
【问题描述】:
在我的脚本的主要目的完成后作为“清理”,调用一个函数以递归方式查看每个文件夹并删除所有以预先确定的扩展名集结尾的文件。
在测试过程中,我发现在要删除的文件列表中带有文件扩展名的一些文件实际上会引发错误:[Errno 1] Operation not permitted: '/location/of/locked/file.png。查看文件本身,它似乎已锁定(在 mac 上)。
-
我将如何使用 Python 从每个文件/文件夹中删除锁定的属性(应该存在),然后如果文件以扩展名结尾,则删除该文件?
最好这可以在下面的同一个函数中完成,因为遍历输入目录需要很长时间 - 只处理一次是要走的路。 -
这如何影响脚本在 Windows 上的完整性?
我已经以一种使其在操作系统之间兼容的方式对其进行了编程,但是(据我所知)locked 属性在 Windows 上并不像在 mac 上那样存在,并且可能会导致未知的一面 -效果。
REMOVE_FILETYPES = ('.png', '.jpg', '.jpeg', '.pdf')
def cleaner(currentPath):
if not os.path.isdir(currentPath):
if currentPath.endswith(REMOVE_FILETYPES) or os.path.basename(currentPath).startswith('.'):
try:
os.remove(currentPath)
print('REMOVED: \"{removed}\"'.format(removed = currentPath))
except BaseException as e:
print('ERROR: Could not remove: \"{failed}\"'.format(failed = str(e)))
finally:
return True
return False
if all([cleaner(os.path.join(currentPath, file)) for file in os.listdir(currentPath)]):
try:
os.rmdir(currentPath)
print('REMOVED: \"{removed}\"'.format(removed = currentPath))
except:
print('ERROR: Could not remove: \"{failed}\"'.format(failed = currentPath))
finally:
return True
return False
cleaner(r'/path/to/parent/dir')
如果有人能告诉我如何将这些功能集成到子程序中,我将不胜感激。干杯。
编辑:根据请求删除错误处理
def cleaner(currentPath):
if sys.platform == 'darwin':
os.system('chflags nouchg {}'.format(currentPath))
if not os.path.isdir(currentPath):
if currentPath.endswith(REMOVE_FILETYPES) or os.path.basename(currentPath).startswith('.'):
try:
os.remove(currentPath)
print('REMOVED: \"{removed}\"'.format(removed=currentPath))
except PermissionError:
if sys.platform == 'darwin':
os.system('chflags nouchg {}'.format(currentPath))
os.remove(currentPath)
if all([cleaner(os.path.join(currentPath, file)) for file in os.listdir(currentPath)]) and not currentPath == SOURCE_DIR:
os.rmdir(currentPath)
print('REMOVED: \"{removed}\"'.format(removed=currentPath))
【问题讨论】:
标签: python python-3.x macos locked-files