分享五個超實用Python腳本,減少垃圾軟件負擔
前言
大家好,我是辣條
今天給大家?guī)韼讉€實用的python腳本工具,原因不難猜這段時間我親愛的女朋友呢給我整出點小花樣,差點讓我電腦GG了。我打開系統(tǒng)盤一看真的是通紅通紅的啊 細細一看一堆的垃圾軟件,關(guān)鍵是她安裝的時候壓根不看附帶一堆的垃圾軟件,這時候我只能掏出我珍藏多年的一些腳本用以避免之后再發(fā)生類似的慘案了
系統(tǒng)提示工具
這個工具用到了win10toast庫來觸發(fā)系統(tǒng)的通知,可以用于提示重要事情。
#定時通知腳本 from win10toast import ToastNotifier import time #構(gòu)建通知對象實例 toaster = ToastNotifier() title = input("請輸入事件標題:") content = input("請輸入事件提要") time_min = float(input("請輸入提醒時間(分鐘):")) #time_min = time_min * 60 print("設置完成!") time.sleep(1) print("開始運行..") time.sleep(time_min) toaster.show_toast(f"{title}", f"{content}", duration=10, threaded=True) while toaster.notification_active(): time.sleep(0.005)
文件夾清理工具
import os import threading import time def get_file_list(file_path): #文件按最后修改時間排序 dir_list = os.listdir(file_path) if not dir_list: return else: dir_list = sorted(dir_list, key=lambda x: os.path.getmtime(os.path.join(file_path, x))) return dir_list def get_size(file_path): """[summary] Args: file_path ([type]): [目錄] Returns: [type]: 返回目錄大小,MB """ totalsize=0 for filename in os.listdir(file_path): totalsize=totalsize+os.path.getsize(os.path.join(file_path, filename)) #print(totalsize / 1024 / 1024) return totalsize / 1024 / 1024 def detect_file_size(file_path, size_Max, size_Del): """[summary] Args: file_path ([type]): [文件目錄] size_Max ([type]): [文件夾最大大小] size_Del ([type]): [超過size_Max時要刪除的大小] """ print(get_size(file_path)) if get_size(file_path) > size_Max: fileList = get_file_list(file_path) for i in range(len(fileList)): if get_size(file_path) > (size_Max - size_Del): print ("del :%d %s" % (i + 1, fileList[i])) #os.remove(file_path + fileList[i]) def detectFileSize(): #檢測線程,每個5秒檢測一次 while True: print('======detect============') detect_file_size("/Users/aaron/Downloads/", 100, 30) time.sleep(5) if __name__ == "__main__": #創(chuàng)建檢測線程 detect_thread = threading.Thread(target = detectFileSize) detect_thread.start()
PDF文件轉(zhuǎn)音頻
import pyttsx3 import pyPDF2 book = open('路徑/book.pdf',rb) pdfreader = pyPDF2.PdfFileReader(book) pages = pdfreader.numPages print(pages) voice = pyttsx3.init() page = pdfreader.getpage(3) text = page.extractText() speaker.say(text) speaker.runAndWait()
批量壓縮文件
import zipfile # zipfile庫 壓縮文件 import os import time def batch_zip(start_dir): start_dir = start_dir #文件路徑 file_news = start_dir + '.zip' # 壓縮后文件夾的名字 z = zipfile.ZipFile(file_news, 'w', zipfile.ZIP_DEFLATED) for dir_path, dir_names, file_names in os.walk(start_dir): #避免從根目錄復制 f_path = dir_path.replace(start_dir, '') #壓縮所有文件 f_path = f_path and f_path + os.sep for filename in file_names: z.write(os.path.join(dir_path, filename), f_path + filename) z.close() return file_news batch_zip('./data/ziptest')
郵件發(fā)送
# 1、導入模塊 import yagmail # 2、設置smtp服務信息 yag = yagmail.SMTP(user="改成自己的郵箱賬號@126.com", password="改成自己的郵箱密碼", host='smtp.126.com') # 3、設置郵件主題與郵件內(nèi)容 subject = 'Python郵件測試' content = ['Python郵件測試 -- 郵件來自黑馬程序員Python+大數(shù)據(jù)'] # 4、發(fā)送郵件 yag.send('gocndws@126.com', subject, content)
以上就是分享五個超實用Python腳本,減少垃圾軟件負擔的詳細內(nèi)容,更多關(guān)于Python腳本的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
django 多數(shù)據(jù)庫及分庫實現(xiàn)方式
這篇文章主要介紹了django 多數(shù)據(jù)庫及分庫實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python函數(shù)中的不定長參數(shù)相關(guān)知識總結(jié)
今天給大家?guī)淼氖顷P(guān)于Python函數(shù)的相關(guān)知識,文章圍繞著Python不定長參數(shù)展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06關(guān)于Python核心框架tornado的異步協(xié)程的2種方法詳解
今天小編就為大家分享一篇關(guān)于Python核心框架tornado的異步協(xié)程的2種方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08python+mongodb數(shù)據(jù)抓取詳細介紹
這篇文章主要介紹了python+mongodb數(shù)據(jù)抓取詳細介紹,具有一定參考價值,需要的朋友可以了解下。2017-10-10