forked from wangandi520/andyspythonscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path识别rarzip内是否有文件名是关键词的文件,(公众号)为例(拖拽,多个文件或文件夹,支持多层文件夹).py
65 lines (60 loc) · 2.34 KB
/
识别rarzip内是否有文件名是关键词的文件,(公众号)为例(拖拽,多个文件或文件夹,支持多层文件夹).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# encoding:utf-8
# https://github.com/wangandi520/andyspythonscript
from pathlib import Path
import sys
import os
import zipfile
import rarfile
def main(inputPath):
del inputPath[0]
# 设置你的关键词
keyword = '公众号'
for aPath in inputPath:
if Path.is_dir(Path(aPath)):
for file in Path(aPath).glob('**/*'):
if file.suffix.lower() == '.rar' and rarfile.is_rarfile(file):
rf = rarfile.RarFile(file)
for eachFile in rf.namelist():
if keyword in eachFile:
print(file)
rf.close()
if file.suffix.lower() == '.zip' and zipfile.is_zipfile(file):
zf = zipfile.ZipFile(file)
for eachFile in zf.namelist():
try:
eachFile = eachFile.encode('cp437').decode('gbk')
except:
eachFile = eachFile.encode('utf-8').decode('utf-8')
if keyword in eachFile:
print(file)
zf.close()
if keyword in file.name:
print(file)
if Path.is_file(Path(aPath)):
file = Path(aPath)
if file.suffix.lower() == '.rar' and rarfile.is_rarfile(file):
rf = rarfile.RarFile(file)
for eachFile in rf.namelist():
if keyword in eachFile:
print(file)
rf.close()
if file.suffix.lower() == '.zip' and zipfile.is_zipfile(file):
zf = zipfile.ZipFile(file)
for eachFile in zf.namelist():
try:
eachFile = eachFile.encode('cp437').decode('gbk')
except:
eachFile = eachFile.encode('utf-8').decode('utf-8')
if keyword in eachFile:
print(file)
zf.close()
if keyword in file.name:
print(file)
print('\n')
os.system('pause')
if __name__ == '__main__':
try:
if len(sys.argv) >= 2:
main(sys.argv)
except IndexError:
pass