分享五個(gè)超實(shí)用Python腳本,減少垃圾軟件負(fù)擔(dān)
前言
大家好,我是辣條
今天給大家?guī)?lái)幾個(gè)實(shí)用的python腳本工具,原因不難猜這段時(shí)間我親愛的女朋友呢給我整出點(diǎn)小花樣,差點(diǎn)讓我電腦GG了。我打開系統(tǒng)盤一看真的是通紅通紅的啊 細(xì)細(xì)一看一堆的垃圾軟件,關(guān)鍵是她安裝的時(shí)候壓根不看附帶一堆的垃圾軟件,這時(shí)候我只能掏出我珍藏多年的一些腳本用以避免之后再發(fā)生類似的慘案了
系統(tǒng)提示工具
這個(gè)工具用到了win10toast庫(kù)來(lái)觸發(fā)系統(tǒng)的通知,可以用于提示重要事情。
#定時(shí)通知腳本 from win10toast import ToastNotifier import time #構(gòu)建通知對(duì)象實(shí)例 toaster = ToastNotifier() title = input("請(qǐng)輸入事件標(biāo)題:") content = input("請(qǐng)輸入事件提要") time_min = float(input("請(qǐng)輸入提醒時(shí)間(分鐘):")) #time_min = time_min * 60 print("設(shè)置完成!") time.sleep(1) print("開始運(yùn)行..") 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): #文件按最后修改時(shí)間排序 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]): [超過(guò)size_Max時(shí)要?jiǎng)h除的大小] """ 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(): #檢測(cè)線程,每個(gè)5秒檢測(cè)一次 while True: print('======detect============') detect_file_size("/Users/aaron/Downloads/", 100, 30) time.sleep(5) if __name__ == "__main__": #創(chuàng)建檢測(cè)線程 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庫(kù) 壓縮文件 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ù)制 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、導(dǎo)入模塊 import yagmail # 2、設(shè)置smtp服務(wù)信息 yag = yagmail.SMTP(user="改成自己的郵箱賬號(hào)@126.com", password="改成自己的郵箱密碼", host='smtp.126.com') # 3、設(shè)置郵件主題與郵件內(nèi)容 subject = 'Python郵件測(cè)試' content = ['Python郵件測(cè)試 -- 郵件來(lái)自黑馬程序員Python+大數(shù)據(jù)'] # 4、發(fā)送郵件 yag.send('gocndws@126.com', subject, content)
以上就是分享五個(gè)超實(shí)用Python腳本,減少垃圾軟件負(fù)擔(dān)的詳細(xì)內(nèi)容,更多關(guān)于Python腳本的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python同時(shí)遍歷數(shù)組的索引和值的實(shí)例
今天小編就為大家分享一篇python同時(shí)遍歷數(shù)組的索引和值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11python實(shí)現(xiàn)三種隨機(jī)請(qǐng)求頭方式
這篇文章主要介紹了python實(shí)現(xiàn)三種隨機(jī)請(qǐng)求頭方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01django 多數(shù)據(jù)庫(kù)及分庫(kù)實(shí)現(xiàn)方式
這篇文章主要介紹了django 多數(shù)據(jù)庫(kù)及分庫(kù)實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04Python函數(shù)中的不定長(zhǎng)參數(shù)相關(guān)知識(shí)總結(jié)
今天給大家?guī)?lái)的是關(guān)于Python函數(shù)的相關(guān)知識(shí),文章圍繞著Python不定長(zhǎng)參數(shù)展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06關(guān)于Python核心框架tornado的異步協(xié)程的2種方法詳解
今天小編就為大家分享一篇關(guān)于Python核心框架tornado的異步協(xié)程的2種方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08python實(shí)現(xiàn)在目錄中查找指定文件的方法
這篇文章主要介紹了python實(shí)現(xiàn)在目錄中查找指定文件的方法,通過(guò)模糊查找與精確查找兩個(gè)實(shí)例較為詳細(xì)的闡述了文件查找的方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11Python字符串和字典相關(guān)操作的實(shí)例詳解
這篇文章主要介紹了Python字符串和字典相關(guān)操作的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-09-09python-web根據(jù)元素屬性進(jìn)行定位的方法
這篇文章主要介紹了python-web根據(jù)元素屬性進(jìn)行定位的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12python+mongodb數(shù)據(jù)抓取詳細(xì)介紹
這篇文章主要介紹了python+mongodb數(shù)據(jù)抓取詳細(xì)介紹,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10