❝
「你还在做重复劳动?醒醒!Python正用它温柔的代码怀抱等待你投向它的怀里。」
❞
还记得那些让你抓狂的日常任务吗?整理文件、回复邮件、清理桌面…这些单调无聊的琐事就像你的前任,总在你忙的时候给你添堵。但别怕!今天我要告诉你,Python,比前任还体贴,它能用简单的代码让这些问题统统自动化!
准备好了吗?接下来是5个Python自动化脚本,让你的人生轻松加倍!
1.「文件自动整理:桌面清爽一键搞定」
你的桌面是不是已经看不清壁纸了?文件东一个、西一个,跟难以捋清的恋爱线一样?
import os
import shutil
def organize_folder(folder_path):
for filename in os.listdir(folder_path):
file_ext = filename.split('.')[-1]
ext_folder = os.path.join(folder_path, file_ext.upper())
if not os.path.exists(ext_folder):
os.makedirs(ext_folder)
shutil.move(os.path.join(folder_path, filename), ext_folder)
organize_folder(r'C:\Users\你的用户名\Desktop')
「脚本作用」:把桌面的文件按扩展名分类,比如图片归一类、文档归一类,整整齐齐像军训宿舍。
「使用后效果」:再也不用担心领导看到你凌乱的桌面截图。
2.「邮件自动回复:拯救社恐的神奇脚本」
再也不用绞尽脑汁想"收到"之外的词了,Python会帮你搞定。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def auto_reply(to_email, subject, body):
from_email = "你的邮箱@gmail.com"
password = "你的邮箱密码"
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login(from_email, password)
server.send_message(msg)
auto_reply("老板@公司.com", "关于那个周报", "好的,我马上就改!")
「脚本作用」:在老板找你之前,优雅地发送邮件,显得你永远在线。
「使用后效果」:老板可能以为你开始修炼"分身术"。
3.「定时喝水提醒:健康从不缺席」
每天坐在电脑前一动不动,干得像沙漠里的骆驼?试试这个贴心的喝水提醒吧。
import time
import ctypes
def remind_to_drink_water():
while True:
time.sleep(3600) # 每小时提醒一次
ctypes.windll.user32.MessageBoxW(0, "快喝水!", "健康小助手", 1)
remind_to_drink_water()
「脚本作用」:每隔一小时弹窗提醒你喝水,让你避免成为"干涸的程序猿"。
「使用后效果」:从程序猿变身程序仙。
4.「批量改名:把文件名从"乱七八糟"改成"整整齐齐"」
你的文件名还在叫New Document (2).docx
?Python帮你重拾秩序!
import os
def batch_rename(folder_path, prefix):
for count, filename in enumerate(os.listdir(folder_path), start=1):
file_ext = filename.split('.')[-1]
new_name = f"{prefix}_{count}.{file_ext}"
os.rename(os.path.join(folder_path, filename), os.path.join(folder_path, new_name))
batch_rename(r'C:\Users\你的用户名\Documents', 'Project')
「脚本作用」:批量给文件取名,不再有羞耻的命名。
「使用后效果」:从"乱起名大师"进阶为"命名艺术家"。
5.「网络爬虫:自动获取你最想要的信息」
比如每天爬取天气预报,决定出门穿什么衣服。
import requests
from bs4 import BeautifulSoup
def get_weather():
url = 'https://www.weather.com/weather/today/l/你的城市代码'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
weather = soup.find('div', class_='CurrentConditions--phraseValue--2xXSr').text
print(f"今天的天气是:{weather}")
get_weather()
「脚本作用」:自动获取天气情况,让你告别出门就挨冻。
「使用后效果」:每一天都成为气象专家!
「总结」
Python就像一个无微不至的生活助理,帮你打理繁杂琐事,给你更多时间摸鱼、追剧、享受生活。和前任不同的是,Python从不抱怨,也不会拉黑你。
还等什么呢?赶紧试试这些脚本,打开生活的新篇章吧!记住:重复劳动交给Python,快乐生活属于你!