Python 实用脚本技巧合集:文件处理、时间计算与桌面提醒
作为日常开发者或运维人员,掌握一些实用的 Python 脚本技巧,可以极大提高工作效率。
本文汇总了常见的文件操作、时间计算及简单桌面提醒示例,助你快速上手自动化日常任务。
一、文件批量处理
1. 批量重命名文件
python
复制编辑
import os folder = "./test_folder" for i, filename in enumerate(os.listdir(folder)): ext = os.path.splitext(filename)[1] new_name = f"文件_{i+1}{ext}" os.rename(os.path.join(folder, filename), os.path.join(folder, new_name)) print("批量重命名完成!")
2. 读取多个文本文件内容合并
python
复制编辑
import glob files = glob.glob("logs/*.txt") with open("合并日志.txt", "w", encoding="utf-8") as outfile: for fname in files: with open(fname, encoding="utf-8") as infile: outfile.write(infile.read() + "\n") print("文件合并完成!")
二、时间日期计算
1. 计算两个日期间天数差
python
复制编辑
from datetime import datetime date1 = datetime.strptime("2025-08-01", "%Y-%m-%d") date2 = datetime.strptime("2025-08-05", "%Y-%m-%d") diff = (date2 - date1).days print(f"相差天数:{diff}")
2. 获取当前时间并格式化输出
python
复制编辑
now = datetime.now() print(now.strftime("当前时间:%Y-%m-%d %H:%M:%S"))
三、简单桌面提醒
Windows 平台使用 ctypes
python
复制编辑
import ctypes ctypes.windll.user32.MessageBoxW(0, "任务完成提醒!", "Python 提示", 0x40 | 0x1)
跨平台使用 plyer
bash
复制编辑
pip install plyer
python
复制编辑
from plyer import notification notification.notify( title="Python 提醒", message="该休息一下啦!", timeout=5 )
四、命令行参数处理
python
复制编辑
import argparse parser = argparse.ArgumentParser(description="示例脚本") parser.add_argument("--name", type=str, help="输入你的名字", required=True) args = parser.parse_args() print(f"你好,{args.name}!欢迎使用脚本。")
运行示例:
bash
复制编辑
python script.py --name 花花
五、日志记录示例
python
复制编辑
import logging logging.basicConfig( filename='app.log', level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S' ) logging.info("程序启动") logging.error("出现错误!")
六、总结
| 技巧类别 | 适用场景 |
|---|---|
| 文件批量处理 | 日常文件整理、日志合并 |
| 时间计算 | 任务调度、时间差统计 |
| 桌面提醒 | 任务完成提示、定时通知 |
| 命令行参数 | 脚本灵活调用、参数传递 |
| 日志记录 | 程序调试、运行监控 |
https://bigu.wang
https://www.bigu.wang
https://binm.wang
https://www.binm.wang
https://bint.wang
https://www.bint.wang
https://biop.wang
https://www.biop.wang
https://bits.wang
https://www.bits.wang
https://bjqb.wang
https://www.bjqb.wang
https://bjsm.wang
https://www.bjsm.wang
https://bleo.wang
https://www.bleo.wang
https://ono.wang
https://www.ono.wang
https://onz.wang
https://www.onz.wang
https://opo.wang
https://www.opo.wang
https://osm.wang
https://www.osm.wang
https://osn.wang
https://www.osn.wang
https://ovi.wang
https://www.ovi.wang
https://oxq.wang
https://www.oxq.wang
https://oti.wang
https://www.oti.wang
https://owu.wang
https://www.owu.wang
https://piq.wang
https://www.piq.wang
https://qmi.wang
https://www.qmi.wang
https://qki.wang
https://www.qki.wang
https://ref.wang
https://www.ref.wang
https://sak.wang
https://www.sak.wang
https://sar.wang
https://www.sar.wang
https://sfa.wang
https://www.sfa.wang
https://sfe.wang
https://www.sfe.wang
https://sgo.wang
https://www.sgo.wang
https://sku.wang
https://www.sku.wang
https://ycxjz.cn
https://www.ycxjz.cn
https://bnbmhomes.cn
https://www.bnbmhomes.cn
https://jinjianzuche.com
https://www.jinjianzuche.com
https://ahswt.cn
https://www.ahswt.cn
https://szwandaj.cn
https://www.szwandaj.cn
https://psbest.cn
https://www.psbest.cn
https://shanghai-arnold.cn
https://www.shanghai-arnold.cn
https://zgsscw.com
https://www.zgsscw.com
https://shxqth.cn
https://www.shxqth.cn
https://wdxj.cn
https://www.wdxj.cn
https://jad168.com
https://www.jad168.com
https://ultratrailms.cn
https://www.ultratrailms.cn
https://tztsjd.cn
https://www.tztsjd.cn
https://csqcbx.cn
https://www.csqcbx.cn
https://qazit.cn
https://www.qazit.cn
https://ahzjyl.cn
https://www.ahzjyl.cn
更多推荐


所有评论(0)