Python是一種強(qiáng)大的編程語言,可以用于編寫自動化腳本,簡化重復(fù)性任務(wù),提高工作效率。下面是一些常見的Python自動化腳本示例:
示例代碼如下: # 文件處理示例:批量重命名文件import osdef batch_rename_files(folder_path, new_name_prefix): for i, filename in enumerate(os.listdir(folder_path)): new_filename = f'{new_name_prefix}_{i 1}.txt' os.rename(os.path.join(folder_path, filename), os.path.join(folder_path, new_filename))# 網(wǎng)絡(luò)爬蟲示例:使用requests庫獲取網(wǎng)頁內(nèi)容import requestsurl = 'https://www.'response = requests.get(url)print(response.text)# 自動發(fā)送電子郵件示例:使用smtplib庫發(fā)送郵件import smtplibfrom email.mime.text import MIMETextdef send_email(subject, message, from_addr, to_addr, smtp_server, smtp_port, smtp_username, smtp_password): msg = MIMEText(message) msg['Subject'] = subject msg['From'] = from_addr msg['To'] = to_addr server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() server.login(smtp_username, smtp_password) server.sendmail(from_addr, to_addr, msg.as_string()) server.quit()# 定時任務(wù)示例:使用APScheduler庫執(zhí)行定時任務(wù)from apscheduler.schedulers.blocking import BlockingSchedulerdef my_job(): print('This is a scheduled job!')scheduler = BlockingScheduler()scheduler.add_job(my_job, 'interval', minutes=10)scheduler.start()# 數(shù)據(jù)處理示例:使用Pandas庫進(jìn)行數(shù)據(jù)處理import pandas as pddata = pd.read_csv('data.csv')# 進(jìn)行數(shù)據(jù)處理操作# 自動化測試示例:使用Selenium庫進(jìn)行Web應(yīng)用自動化測試from selenium import webdriverdriver = webdriver.Chrome()driver.get('https://www.')# 進(jìn)行自動化測試操作# 自動化辦公任務(wù)示例:使用openpyxl庫處理Excel文件import openpyxlwb = openpyxl.load_workbook('example.xlsx')sheet = wb.active# 進(jìn)行Excel文件處理操作wb.save('example_output.xlsx') |
|