Python 凭借其简洁的语法和强大的生态系统,成为自动化任务和提升工作效率的绝佳工具。无论是文件处理、数据操作,还是日常事务的自动化,几行 Python 代码常常能帮你节省数小时的手动工作。
本文整理了 20 个极其实用的 Python 脚本,涵盖文件操作、数据处理、网络任务和系统自动化等多个方面,并附上代码示例,方便你直接复制使用,让你的生产力瞬间飙升!
1. 批量重命名文件问题:需要给一个文件夹里的所有图片加上前缀。 解决方案:使用 os
模块列出并重命名文件。
import os
def batch_rename(path, prefix):
"""为指定目录下的所有文件添加前缀"""
for count, filename in enumerate(os.listdir(path)):
old_path = os.path.join(path, filename)
if os.path.isfile(old_path):
# 分离文件名和扩展名
name, ext = os.path.splitext(filename)
new_name = f"{prefix}_{name}{ext}"
new_path = os.path.join(path, new_name)
os.rename(old_path, new_path)
print("重命名完成!")
# 使用示例
batch_rename("./photos", "vacation")
2. 遍历目录及其子目录下的所有文件问题:需要处理一个嵌套很深的文件夹里的所有特定类型文件。 解决方案:使用 os.walk()
。
import os
def list_all_files(directory):
"""列出目录下所有文件的完整路径"""
for root, dirs, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
print(file_path)
# 使用示例
list_all_files("./projects")
3. 文件搜索器问题:如何在大量文件中快速找到包含特定关键词的文件? 解决方案:读取文件内容并进行匹配。
def search_in_files(directory, keyword):
"""搜索目录下所有包含关键词的文件"""
for root, dirs, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
if keyword in content:
print(f"在文件中找到关键词 '{keyword}': {file_path}")
except Exception as e:
# 忽略无法读取的文件(如二进制文件)
pass
# 使用示例
search_in_files("./docs", "Python脚本")
4. 创建目录(如果不存在)问题:在写文件前,需要确保其所在的目录存在,避免报错。 解决方案:使用 os.makedirs(exist_ok=True)
。
import os
def safe_write_to_file(file_path, content):
"""安全地写入文件,自动创建所需目录"""
directory = os.path.dirname(file_path)
if directory and not os.path.exists(directory):
os.makedirs(directory, exist_ok=True) # exist_ok=True 防止目录已存在时报错
with open(file_path, 'w') as f:
f.write(content)
print(f"文件已写入: {file_path}")
# 使用示例
safe_write_to_file("./output/logs/app.log", "这是一条日志信息。")
5. CSV 转 JSON问题:如何将 CSV 数据转换为 JSON 格式以供其他应用使用? 解决方案:使用 csv
和 json
模块。
import csv
import json
def csv_to_json(csv_file, json_file):
"""将CSV文件转换为JSON文件"""
data = []
with open(csv_file, 'r', encoding='utf-8') as f:
csv_reader = csv.DictReader(f)
for row in csv_reader:
data.append(row)
with open(json_file, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4, ensure_ascii=False)
print(f"转换完成!JSON文件已保存至: {json_file}")
# 使用示例
csv_to_json("input.csv", "output.json")
6. Excel 表格读取与汇总问题:需要从多个 Excel 工作表中提取数据并做简单汇总。 解决方案:使用 pandas
库(需安装:pip install pandas
)。
import pandas as pd
def summarize_excel(file_path, output_file):
"""读取Excel文件,对每个工作表进行描述性统计,并保存到新文件"""
with pd.ExcelWriter(output_file) as writer:
# sheet_name=None 会读取所有工作表
all_sheets = pd.read_excel(file_path, sheet_name=None)
for sheet_name, df in all_sheets.items():
# 对每个工作表进行描述性统计
summary = df.describe(include='all')
summary.to_excel(writer, sheet_name=f"Summary_{sheet_name}")
print(f"汇总完成!文件已保存至: {output_file}")
# 使用示例
summarize_excel("data.xlsx", "summary_report.xlsx")
7. 简单的数据清洗问题:数据中有空值或异常值需要处理。 解决方案:使用 pandas
进行快速清洗。
import pandas as pd
def clean_data(input_file, output_file):
"""数据清洗:填充空值并过滤掉异常值"""
df = pd.read_csv(input_file)
# 填充数字列的空值为中位数
for col in df.select_dtypes(include=['number']).columns:
df[col].fillna(df[col].median(), inplace=True)
# 过滤掉“年龄”列中大于100的异常值
if'age'in df.columns:
df = df[df['age'] <= 100]
df.to_csv(output_file, index=False)
print(f"数据清洗完成!已保存至: {output_file}")
# 使用示例
clean_data("dirty_data.csv", "cleaned_data.csv")
8. 简单的网页下载器问题:如何快速下载一个文件? 解决方案:使用 requests
库(需安装:pip install requests
)。
import requests
def download_file(url, local_filename):
"""下载文件并保存到本地"""
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
print(f"文件已下载: {local_filename}")
# 使用示例
download_file('https://example.com/somefile.zip', 'downloaded_file.zip')
9. 监控网站状态问题:需要定期检查网站是否可访问。 解决方案:发送 HEAD 请求检查状态码。
import requests
def check_website(url):
"""检查网站状态"""
try:
response = requests.head(url, timeout=5)
print(f"{url} 的状态码是: {response.status_code}")
return response.status_code == 200
except requests.RequestException as e:
print(f"{url} 无法访问: {e}")
return False
# 使用示例
check_website("https://www.google.com")
10. 发送邮件通知问题:脚本运行完成后,如何自动发送邮件通知? 解决方案:使用 smtplib
和 email
模块。
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def send_email(sender, receivers, subject, content, smtp_server, password):
"""发送邮件通知"""
message = MIMEText(content, 'plain', 'utf-8')
message['From'] = Header(sender, 'utf-8')
message['To'] = Header(",".join(receivers), 'utf-8')
message['Subject'] = Header(subject, 'utf-8')
try:
smtp_obj = smtplib.SMTP_SSL(smtp_server) # 例如QQ邮箱:smtp.qq.com, 端口465
smtp_obj.login(sender, password)
smtp_obj.sendmail(sender, receivers, message.as_string())
smtp_obj.quit()
print("邮件发送成功")
except smtplib.SMTPException as e:
print(f"无法发送邮件: {e}")
# 使用示例(需配置发件人邮箱和授权码)
# send_email('sender@qq.com', ['receiver@example.com'], '脚本运行完成', '数据处理任务已成功执行!', 'smtp.qq.com', 'your_authorization_code')
11. 桌面通知(Windows)问题:长时间运行的脚本完成后,如何提醒自己? 解决方案:使用 win10toast
库(需安装:pip install win10toast
)。
from win10toast import ToastNotifier
import time
def desktop_notify(title, message, duration=5):
"""发送Windows桌面通知"""
toaster = ToastNotifier()
toaster.show_toast(title, message, duration=duration)
# 等待通知结束
time.sleep(duration)
# 使用示例
desktop_notify("脚本通知", "数据处理已完成!")
12. 环境变量检查器问题:调试时不确定环境变量是否正确设置。 解决方案:打印出所有或特定的环境变量。
import os
def check_env_vars(*variables):
"""检查一个或多个环境变量是否存在及其值"""
for var in variables:
value = os.getenv(var)
if value is None:
print(f"环境变量 '{var}' 未设置。")
else:
print(f"{var} = {value}")
# 使用示例:检查JAVA_HOME和PATH
check_env_vars('JAVA_HOME', 'PATH')
13. 获取当前天气(简易版)问题:想在开始工作前快速了解一下天气。 解决方案:调用一个免费的天气 API(示例使用 requests
)。
import requests
def get_weather(city_name, api_key):
"""获取指定城市的天气信息(这里以OpenWeatherMap为例)"""
base_url = "http://api.openweathermap.org/data/2.5/weather?"
complete_url = f"{base_url}appid={api_key}&q={city_name}&units=metric&lang=zh_cn"
response = requests.get(complete_url)
data = response.json()
if data["cod"] != "404":
main = data["main"]
weather_desc = data["weather"][0]["description"]
temp = main["temp"]
print(f"{city_name}的天气: {weather_desc}")
print(f"当前温度: {temp}°C")
else:
print("城市未找到")
# 使用示例(需要去OpenWeatherMap申请免费API Key)
# get_weather("Beijing", "YOUR_API_KEY_HERE")
14. 执行系统命令并获取输出问题:需要在 Python 脚本中运行一个系统命令。 解决方案:使用 subprocess
模块。
import subprocess
def run_command(command):
"""执行系统命令并打印输出"""
try:
# capture_output=True 捕获输出,text=True 返回字符串而非字节
result = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=30)
print("STDOUT:")
print(result.stdout)
if result.stderr:
print("STDERR:")
print(result.stderr)
print(f"返回码: {result.returncode}")
except subprocess.TimeoutExpired:
print("命令执行超时!")
# 使用示例
run_command("dir") # Windows
# run_command("ls -l") # Linux/Mac
15. 生成随机密码问题:需要快速生成一个安全的随机密码。 解决方案:使用 secrets
模块(比 random
更安全)。
import secrets
import string
def generate_password(length=12):
"""生成一个包含大小写字母、数字和符号的随机密码"""
alphabet = string.ascii_letters + string.digits + "!@#$%^&*"
password = ''.join(secrets.choice(alphabet) for i in range(length))
return password
# 使用示例
print(f"你的新密码是: {generate_password(16)}")
16. 计算代码运行时间问题:想比较不同算法或函数的性能。 解决方案:使用 time
模块或装饰器。
import time
def timing_decorator(func):
"""一个简单的计时装饰器"""
def wrapper(*args, **kwargs):
start_time = time.perf_counter()
result = func(*args, **kwargs)
end_time = time.perf_counter()
elapsed_time = end_time - start_time
print(f"函数 {func.__name__} 运行耗时: {elapsed_time:.4f} 秒")
return result
return wrapper
# 使用示例:在要计时的函数上加上 @timing_decorator 即可
@timing_decorator
def example_function():
time.sleep(1)
example_function()
17. 提取 PDF 文本(简易)问题:想从 PDF 文件中快速提取文字。 解决方案:使用 PyPDF2
库(需安装:pip install PyPDF2
)。
import PyPDF2
def extract_text_from_pdf(pdf_path):
"""从PDF第一页提取文本"""
with open(pdf_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
if len(reader.pages) > 0:
first_page = reader.pages[0]
text = first_page.extract_text()
return text
else:
return"PDF为空或无法读取。"
# 使用示例
text = extract_text_from_pdf("document.pdf")
print(text)
18. 字符串搜索与替换(支持正则)问题:需要在文本中进行复杂的查找和替换。 解决方案:使用 re
模块进行正则表达式操作。
import re
def advanced_replace(text, pattern, replacement):
"""使用正则表达式进行高级搜索替换"""
new_text = re.sub(pattern, replacement, text)
return new_text
# 使用示例
original_text = "我的电话是 123-4567-8901,另一个是 987-6543-210。"
# 匹配所有XXX-XXXX-XXX模式的电话号
pattern = r'\d{3}-\d{4}-\d{3,4}'
replacement = "【电话号码已隐藏】"
result = advanced_replace(original_text, pattern, replacement)
print(result)
19. 简单的 HTTP 服务器问题:如何快速在同一网络下共享文件? 解决方案:使用 Python 内置的 HTTP 服务器。
# 这不是一个.py脚本,而是在命令行中直接运行的命令
# 在需要共享的目录下打开终端(CMD或PowerShell),运行:
python -m http.server 8000
# 然后同一局域网下的设备浏览器访问 http://你的IP地址:8000 即可
20. JSON 和 Python 对象的漂亮打印问题:查看复杂的 JSON 或字典数据时很混乱。 解决方案:使用 pprint
模块。
import json
from pprint import pprint
# 假设有一个复杂的嵌套字典或JSON字符串
complex_data = {'name': 'John', 'age': 30, 'pets': [{'type': 'dog', 'name': 'Spot'}, {'type': 'cat', 'name': 'Fluffy'}], 'address': {'street': '123 Main St', 'city': 'Anytown'}}
print("普通打印:")
print(complex_data) # 一行显示,难以阅读
print("\n漂亮打印:")
pprint(complex_data, indent=4, width=50) # 格式化输出,清晰易读
# 如果是JSON字符串,可以先加载
# json_data = json.loads(json_string)
# pprint(json_data)
掌握这些脚本并融入到你的日常工作流程中,你会发现Python不仅可以应对复杂的数据科学任务,更是自动化繁琐工作的“瑞士军刀”。从今天开始,尝试用脚本代替手动操作,享受效率飙升带来的乐趣吧!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。