[Python] 纯文本查看 复制代码
import os
import time
import json
import winreg
import configparser
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# 获取程序绝对路径
#APP_PATH = os.path.abspath(__file__)
APP_PATH = os.path.abspath(sys.argv[0])
DIR_PATH = os.path.dirname(APP_PATH)
#print(APP_PATH)
# 读取配置文件
config = configparser.ConfigParser()
config_path = DIR_PATH+'\config.ini'
config.read(config_path)
# 获取用户文档路径
import winreg
# 定义 Documents 文件夹的注册表键路径
#documents_folder_key_path = r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
# 定义 FileSavePath 文件夹的注册表键路径
documents_folder_key_path = r"Software\Tencent\WeChat"
# 打开 Documents 文件夹的注册表键
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, documents_folder_key_path) as key:
# 读取 Documents 文件夹的值
user_documents_path, _ = winreg.QueryValueEx(key, "FileSavePath")
if user_documents_path == "MyDocument:":
# 定义 Documents 文件夹的注册表键路径
documents_folder_key_path = r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
# 打开 Documents 文件夹的注册表键
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, documents_folder_key_path) as key:
# 读取 Documents 文件夹的值
user_documents_path, _ = winreg.QueryValueEx(key, "Personal")
user_documents_path = user_documents_path + "\\WeChat Files"
# 输出 Documents 文件夹的位置
print("微信保存文件夹位置:", user_documents_path)
# 获取子文件夹下所有以 "wxid" 开头的文件夹中的 "FileStorage\File" 文件夹
parent_folder = user_documents_path # 根据需求设置父文件夹路径
target_folders = []
for dirpath, dirnames, filenames in os.walk(parent_folder):
for dirname in dirnames:
if dirname.startswith('wxid') :
target_folder = os.path.join(dirpath, dirname, 'FileStorage\\File')
if os.path.exists(target_folder):
target_folders.append(target_folder)
print('目标文件夹列表:', target_folders)
# 加载已处理过的文件列表
processed_files = {}
json_path = DIR_PATH+'\processed_files.json'
if os.path.exists(json_path):
with open(json_path, 'r') as f:
processed_files = json.load(f)
# 解除目标文件夹及其子文件夹下所有文件的只读状态
for target_folder in target_folders:
for dirpath, dirnames, filenames in os.walk(target_folder):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
if file_path in processed_files and processed_files[file_path]:
continue
os.chmod(file_path, 0o777) # 解除文件只读状态
processed_files[file_path] = True # 标记文件已处理
print('解除只读状态:', file_path)
# 保存已处理过的文件列表
try:
with open(json_path, 'w') as f:
json.dump(processed_files, f)
except Exception as e:
print("写入失败:", e)
# 监听目标文件夹及其子文件夹下新增文件,并解除只读状态
class FileEventHandler(FileSystemEventHandler):
def on_moved(self, event):
if event.is_directory:
return
time.sleep(0.1)
file_path = event.dest_path
if file_path in processed_files and processed_files[file_path]:
return
os.chmod(file_path, 0o777) # 解除新增文件的只读状态
print('解除新增文件只读状态:', file_path)
event_handler = FileEventHandler()
observer = Observer()
for target_folder in target_folders:
observer.schedule(event_handler, path=target_folder, recursive=True)
observer.start()
print('开始监听文件夹...')
def remove_from_startup():
key_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_path, 0, winreg.KEY_ALL_ACCESS) as key:
try:
winreg.DeleteValue(key, "ChatGPT")
print("已取消开机自启")
except FileNotFoundError:
print("未设置开机自启,不需取消")
# 设置开机自启
if config.getboolean('AUTO_STARTUP', 'enabled'):
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", 0, winreg.KEY_ALL_ACCESS)
winreg.SetValueEx(key, "ChatGPT", 0, winreg.REG_SZ, APP_PATH)
winreg.CloseKey(key)
print('已设置开机自启,若要取消,修改config.ini内的true为false')
else:
remove_from_startup()
print('已取消开机自启,若要设置开机自启,修改config.ini内的false为true')
# 程序保持运行状态,等待事件的发生
try:
while observer.is_alive():
observer.join(1)
except KeyboardInterrupt:
observer.stop()
observer.join()