for item in os.listdir(desktop): if item != "Organized": shutil.move(os.path.join(desktop, item), os.path.join(organized, item)) print(f"Moved: {item}")
clean_desktop()
用途:将桌面文件统一移动到“Organized”文件夹中,保持工作区整洁。
5. 离线语音命令识别
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import speech_recognition as sr
def voice_command(): r = sr.Recognizer() with sr.Microphone() as source: print("Speak now...") audio = r.listen(source) try: command = r.recognize_google(audio) print(f"You said: {command}") except: print("Could not understand audio.")
voice_command()
用途:通过语音输入直接与 Python 交互,无需额外 API。
6. 基于文件内容的智能重命名
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import os
def smart_rename(folder): for filename in os.listdir(folder): full_path = os.path.join(folder, filename) if filename.endswith('.txt'): with open(full_path, 'r') as file: first_line = file.readline().strip().replace(" ", "_")[:30] new_name = f"{first_line}.txt" os.rename(full_path, os.path.join(folder, new_name)) print(f"Renamed to: {new_name}")
smart_rename('/path/to/txt/files')
用途:根据文件首行内容自动命名,避免多个 notes.txt 混淆。
7. 电量提醒工具
1 2 3 4 5 6 7 8 9 10 11 12
import psutil import time
def battery_alert(): while True: battery = psutil.sensors_battery() if battery.percent < 15 and not battery.power_plugged: print("⚠️ Battery low! Plug in the charger.") time.sleep(60)
battery_alert()
用途:在电量低于 15% 且未充电时提醒用户,减少意外关机风险。
8. 离线语法检查
1 2 3 4 5 6 7 8 9
from gingerit.gingerit import GingerIt
def fix_grammar(text): parser = GingerIt() result = parser.parse(text) print(result['result'])
fix_grammar("I has a pen and she have a notebook")
用途:本地修正语法错误,保护数据隐私。
9. 银行短信账单解析
快速提取消费记录
1 2 3 4 5 6 7 8 9 10 11 12
import re
def extract_expenses(file_path): with open(file_path, 'r') as f: lines = f.readlines() for line in lines: if "debited" in line.lower(): amount = re.findall(r'INR\s?[\d,]+', line) print(f"Expense: {amount[0] if amount else 'Unknown'} - {line.strip()}")
extract_expenses('bank_sms.txt')
用途:从导出的银行短信记录中提取每笔支出金额,便于财务管理。
10. 基于日历文件的会议提醒
1 2 3 4 5 6 7 8 9 10
from ics import Calendar
def upcoming_events(ics_file): with open(ics_file, 'r') as f: calendar = Calendar(f.read()) for event in calendar.events: print(f"Event: {event.name} | {event.begin.humanize()}")
upcoming_events('calendar.ics')
用途:读取 .ics 日历文件,提醒即将到来的会议安排。
11. 文件夹本地 Web 图库
1 2 3 4 5 6 7 8 9 10 11
import os from http.server import SimpleHTTPRequestHandler, HTTPServer
def serve_folder(folder, port=8000): os.chdir(folder) server = HTTPServer(('localhost', port), SimpleHTTPRequestHandler) print(f"Serving {folder} at http://localhost:{port}") server.serve_forever()
serve_folder('/path/to/image/folder')
用途:将本地文件夹通过局域网以网页形式共享。
12. 文本摘要工具
1 2 3 4 5 6 7 8 9
from gensim.summarization import summarize
def summarize_text(file_path): with open(file_path, 'r') as f: content = f.read() print(summarize(content))