python文件解除占用_如何使用Python解锁锁定的文件和文件夹(mac)

在我的脚本的主要目的完成后,作为“清理”,调用一个函数来递归查看每个文件夹并删除以预定的一组扩展名结尾的所有文件.

我在测试期间发现一些文件扩展名在删除列表中的文件实际上会抛出一个错误:[Errno 1]不允许操作:’/ location / of / locked / file.png.查看文件本身,它似乎是锁定(在Mac上).

>如何使用Python从每个文件/文件夹中删除锁定的属性(如果存在),然后删除文件,如果它在扩展名中结束?

优选地,这可以在下面的相同功能中完成,因为遍历输入目录需要很长时间 – 只需处理一次即可.

>这如何影响Windows上脚本的完整性?

我已经开始对它进行编程,使其在操作系统之间兼容,但是(据我所知),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))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想加密一个文件夹或zip文件,可以使用Python中的加密库来实现。以下是一些常用的加密库: 1. pyAesCrypt:这是一个用于加密和解密文件Python库。它使用AES加密算法,并且可以处理大文件。 2. cryptography:这是一个用于加密和解密数据的Python库。它支持多种加密算法,包括AES、DES、RSA等。 3. PyCrypto:这是一个Python密码库,支持多种加密算法,包括AES、DES、RSA、Blowfish等。 下面是使用pyAesCrypt加密文件夹或zip文件的代码示例: ```python import pyAesCrypt import os # 定义加密函数 def encrypt_folder(password, src_folder, dest_zip): bufferSize = 64 * 1024 pyAesCrypt.encryptFile(src_folder, dest_zip, password, bufferSize) # 定义解密函数 def decrypt_folder(password, src_zip, dest_folder): bufferSize = 64 * 1024 pyAesCrypt.decryptFile(src_zip, dest_folder, password, bufferSize) # 加密文件夹 password = "mypassword" src_folder = "myfolder" dest_zip = "myfolder.zip" encrypt_folder(password, src_folder, dest_zip) # 解密文件夹 src_zip = "myfolder.zip" dest_folder = "myfolder_decrypted" decrypt_folder(password, src_zip, dest_folder) ``` 在上面的代码中,我们使用pyAesCrypt库来加密和解密文件夹。首先,我们定义了两个函数:encrypt_folder和decrypt_folder,分别用于加密和解密文件夹。接着,我们定义了一个密码变量和两个文件路径变量,用于指定要加密的文件夹和加密后的zip文件。最后,我们调用encrypt_folder函数来加密文件夹调用decrypt_folder函数来解密文件夹

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值