forked from Knighthood2001/wechat_OCR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdata_path.py
69 lines (60 loc) · 2.47 KB
/
updata_path.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
import os
import re
import time
from pathlib import Path
def get_updata_path(initial_path):
if not os.path.exists(initial_path):
print("微信已经更新,正在为您更换路径...")
parent_directory = Path(initial_path).parent # 获取上一级目录"G:/applicationsoftware/WeChat"
for item in os.listdir(parent_directory):
# 定义正则表达式模式
pattern = r'^\[(.*)\]$'
# 使用 re.match 进行匹配
match = re.match(pattern, item)
# 判断是否匹配成功
if match:
# 拼接完整路径
item_path = os.path.join(parent_directory, item)
print(item_path)
return item_path
else:
return None
# def update_path():
# # 打开文件并读取第二行内容
# with open('path.txt', 'r') as file:
# lines = file.readlines()
# if len(lines) < 2: # 检查文件是否至少有两行
# raise ValueError("文件内容不足两行,请检查路径")
# if len(lines) == 2:
# second_line = lines[1].strip() # 获取第二行并去除首尾空白符
# content = get_updata_path(second_line)
# if content:
# lines[1] = content + '\n' # 注意要添加换行符
# # 将修改后的内容写回文件
# with open("path.txt", 'w') as file:
# file.writelines(lines)
# else:
# return None
def update_path():
try:
# 打开文件并读取第二行内容
with open('path.txt', 'r') as file:
lines = file.readlines()
if len(lines) < 2: # 检查文件是否至少有两行
raise ValueError("文件内容不足两行,请检查路径")
if len(lines) == 2:
second_line = lines[1].strip() # 获取第二行并去除首尾空白符
content = get_updata_path(second_line)
if content:
lines[1] = content + '\n' # 注意要添加换行符
# 将修改后的内容写回文件
with open("path.txt", 'w') as file:
file.writelines(lines)
else:
return None
except ValueError as e:
print(e)
time.sleep(1) # 暂停1秒
raise # 重新抛出异常,让异常继续传播
if __name__ == "__main__":
update_path()