python win32选取文件夹_为 Zotero 删除 storage 中的“空”文件夹

在 Zotero 中使用 Zotfile 时,常会生成一些空文件夹留在<数据存储位置>/storage中,虽然不占空间,但依然有用户想要删除它们。

这些“空”文件夹通常并不是真的没有文件。附件进入 storage 后,Zotero 缓存全文生成 .zotero-ft-cache,而后附件被 Zotfile 移动至其他位置,留下只包含 .zotero-ft-cache(Linux,MacOS 下为隐藏文件)

以下系统测试通过:

  • Windows 10
  • macOS 10.13.6
  • Manjaro 17.1.11

初稿截至时,由 miniconda 安装的 Python3.6.6 在以上三个系统的中测试通过,Python2.X 在 macOS 系统测试通过。
建议安装 click,否则删除文件夹前不进行确认

笔者强烈建议任何系统装 Python 直接上 minicoda。
#!/usr/bin/env python
# coding: utf-8

from __future__ import print_function

import configparser
import re
import shutil
import sys

try:
    from pathlib import Path
except ImportError:
    from pathlib2 import Path


def get_zotero_storage_dir():
    '''
    Get the Zotero storage dir and in PosixPath type
    '''
    profile_dirs = {
        'darwin': Path.home() / 'Library/Application Support/Zotero',
        'linux': Path.home() / '.zotero/zotero',
        'linux2': Path.home() / '.zotero/zotero',
        'win32': Path.home() / 'AppData/Roaming/Zotero/Zotero'
    }
    profile_dir = profile_dirs[sys.platform]

    config = configparser.ConfigParser()
    config.read('{}'.format(profile_dir / 'profiles.ini'))
    configs_loc = profile_dir / config['Profile0']['Path'] / 'prefs.js'
    configs = configs_loc.read_text()

    zotero_data_pat = re.compile(
        r'user_pref("extensions.zotero.dataDir", "(?P<zotero_data>.+)");')
    zotero_data_dir = Path(zotero_data_pat.search(configs).group('zotero_data'))
    storage_dir = zotero_data_dir / 'storage'
    return storage_dir


def get_empty_folders(zotero_storage_dir):
    '''
    Get a list of empty dir in string type.
    '''
    return [
        p.as_posix() for p in zotero_storage_dir.iterdir()
        if (not p.is_file()) and (
            not len([f for f in list(p.iterdir()) if f.name[0] != '.']))
    ]


if __name__ == '__main__':
    zotero_storage_dir = get_zotero_storage_dir()
    dirs_to_remove = get_empty_folders(zotero_storage_dir)
    try:
        import click
        print('The following folders contain no attachments:')
        print('n  '.join([''] + dirs_to_remove))
        if click.confirm('Do you want remove them?', default=True):
            [shutil.rmtree(p, ignore_errors=True) for p in dirs_to_remove]
    except ImportError:
        print(
            'The following folders containing no attachments will be removed:')
        print('n  '.join([''] + dirs_to_remove))
        [shutil.rmtree(p, ignore_errors=True) for p in dirs_to_remove]

以上脚本也发布在 GithubGist - zot_rm_empty_folders.py,如有 bug 或者功能上的补充,欢迎在 gist 上 pr。同时欢迎文末留言,但如安装 python 或者已有 python 不知安装 click 这类问题,还望自行搜索引擎。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值