题记部分
一、文件夹&文件
(1)删除空文件夹
# 删除目录中的空文件夹
import os
def move_epty_folders(directory_path):
for root, dirs, files in os.walk(directory_path, topdown=False):
for folder in dirs:
folder_path = os.path.join(root, folder)
if not os.listdir(folder_path):
os.rmdir(folder_path)
(2)批量重命名文件
# 批量重命名目录中的文件
import os
def rename_files(directory_path, old_name, new_name):
for filename in os.listdir(directory_path):
if old_name in filename:
new_filename = filename.replace(old_name, new_name)
os.rename(os.path.join(directory_path,filename),os.path.join(directory_path, new_filename))
二、爬虫
(1)从网站中提取数据
import requests
from bs4 import BeautifulSoup
def scrape_data(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 在此处编写代码, 从网站中提取相关数据
(2)批量下载图片
import requests
def download_images(url, save_directory):
response = requests.get(url)
if response.status_code == 200:
# 假设API返回图片URL的json数组
images = response.json()
for index, image_url in enumerate(images):
image_response = requests.get(image_url)
if image_response.status_code == 200:
with open(f"{save_directory}/image_{index}.jpg","wb") as f:
f.write(image_response.content)
(3)自动提交表单
# 自动在网站上提交表单
import requests
def submit_form(url, form_data):
response = requests.post(url, data=form_data)
if response.status_code == 200 :
# 在此处编写代码以处理表单提交侯的响应
print("harley")
三、文本处理和操作
(1)统计文本文件中的单词数
# 统计文本文件中的单词数
def count_words(file_path):
with open(file_path, 'r') as f:
text = f.read()
word_count = len(text.split())
return word_count
file_path = "D:\\houhuilin\\07_project\\pyspace\\python脚本大全\\wordcount.txt"
res = count_words(file_path)
print(f"{file_path}中的单词数: ",res)
(2)查找和替换文本
# 在文件中查找和替换文本
def find_replace(file_path, search_text, replace_text):
# 查找替换
with open(file_path, 'r') as f:
text = f.read()
modified_text = text.replace(search_text,replace_text)
# 写入
with open(file_path, 'w') as f:
f.write(modified_text)
(3)生成随机文本
# 生成随机文本
import random
import string
def generate_random_text(length):
letters = string.ascii_letters + string.digits + string.punctuation
random_text = ''.join(random.choice(letters) for i in range(length))
return random_text
text = generate_random_text(10)
print("生成的随机文本为: "+text)
四、电子邮件
(1)发送个性化电子邮件
# 用于向收件人列表发送个性化邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_personalized_email(sender_email, sender_password, recipients, subject, body):
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls
server.login(sender_email, sender_password)
for recipient_email in recipients:
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = recipient_email
message['Subject'] = subject
message.attach(MIMEText(body,'plain'))
server.sendmail(sender_email, recipient_email, message.as_string())
server.quit()
(2)发送带附件的电子邮件
(3)自动电子邮件提醒
五、Excel电子表格
(1)读写Excel
(2)数据分析和可视化
(3)合并多个表格
六、数据库交互
(1)连接数据库
(2)执行SQL查询
(3)数据备份和恢复
七、自动化系统任务
(1)管理系统进程
(2)使用Cron安排任务
(3)监控磁盘空间
— 业精于勤荒于嬉,行成于思毁于随 —
标签:12,一文,python,text,response,import,path,os,email From: https://www.cnblogs.com/houhuilinblogs/p/18285233