shinemic

Python 2 去除 SumatraPDF 设置无用路径

Oct 30th, 2019
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. from __future__ import unicode_literals
  2.  
  3. import io
  4. import re
  5. import os
  6. import sys
  7.  
  8. def parse(file, encoding='UTF-8-SIG'):
  9.     valid = []
  10.     text = io.open(file, encoding=encoding).read()
  11.     nav_whole = re.match(r'(.*?)(FileStates \[.*?^\])(.*?\Z)', text, re.S | re.M)
  12.     chunk_pat = re.compile(r'(?P<prev>\s*\[\s*FilePath = )(?P<path>.*?$)(?P<last>.*?Favorites \[.*?\].*?\]\n)', re.M | re.S)
  13.     for mat in chunk_pat.finditer(nav_whole.group(2)):
  14.         if os.path.exists(mat.group('path')):
  15.             valid.append(mat.group())
  16.     with io.open(file, 'w', encoding=encoding) as outfile:
  17.         outfile.write(nav_whole.group(1))
  18.         outfile.write('FileStates [\n')
  19.         outfile.writelines(valid)
  20.         outfile.write(']')
  21.         outfile.write(nav_whole.group(3))
  22.  
  23. if __name__ == '__main__':
  24.     if len(sys.argv) == 2:
  25.         parse(sys.argv[1])
Add Comment
Please, Sign In to add comment