使用Python自制一個(gè)回收站清理器
經(jīng)常筆記本電腦的回收站存儲(chǔ)了很多的文件,需要打開回收站全部選中進(jìn)行清理。
但是要是做成python自動(dòng)化,再將其配置成定時(shí)任務(wù)就不需要再去手動(dòng)操作了。或者就是直接雙擊運(yùn)行即可完成所有回收站的文件清理。
由于實(shí)現(xiàn)過(guò)程需要調(diào)用windows的終端命令,所以需要安裝winshell。然而有的小伙伴沒(méi)有安裝pypiwin32就會(huì)報(bào)錯(cuò)沒(méi)有win32con模塊,因此,新的python環(huán)境安裝這兩個(gè)非標(biāo)準(zhǔn)庫(kù)就OK了。
pip?install?winshell pip?install?pypiwin32
其實(shí)真正可以使用到的代碼塊只有一行,就是直接調(diào)用終端完成回收站的清理操作。try…catch只是為了捕獲異常,防止直接拋出。
#?It's?importing?the?winshell?module. import?winshell try: ????print('正在清理回收站所有文件......') ????#?It's?emptying?the?recycle?bin. ????winshell.recycle_bin().empty(confirm=False,?show_progress=False,?sound=True) ????print('回收站已經(jīng)清理完成!') except: ????print('回收站已經(jīng)被清空,不需要操作!') input("請(qǐng)按任意鍵關(guān)閉窗口...")
上述的代碼塊已經(jīng)能夠完成功能了,然后直接使用pyinstaller打包成exe,項(xiàng)目?jī)?nèi)容較小這里直接采用單文件方式進(jìn)行打包。
pyinstaller?-F?-i?.\recycle.ico?.\recycle.py
程序打包完成后生成recycle.exe,可以修改成任意名稱.exe,雙擊執(zhí)行即可完成回收站清理。
recycle.exe
知識(shí)補(bǔ)充
除了上文,小編還給大家整理了用Python實(shí)現(xiàn)的其他實(shí)用小工具,希望對(duì)大家有所幫助
批量圖片格式轉(zhuǎn)換器
經(jīng)常在不同的工作場(chǎng)景中需要使用不同的圖片格式來(lái)完成相應(yīng)的操作,因此開發(fā)了這款批量轉(zhuǎn)換圖片格式的操作工具。
下文介紹的圖片轉(zhuǎn)換過(guò)程主要包含了四種圖片格式的相互轉(zhuǎn)換,分別是jpeg/jpg/png/bmp,通過(guò)獲取Image圖片對(duì)象從而保存為任意的圖片格式。
今天使用到的圖片處理庫(kù)就是PIL,不僅包含了強(qiáng)大的圖像處理能力,還可用于圖像歸檔/批量處理等方面。
pip?install?pillow
然后,直接將PIL模塊的Image模塊導(dǎo)入到我們的代碼塊中,注意這里安裝的名稱是pillow,但是導(dǎo)入的模塊名稱卻是PIL。
#?Importing?the?Image?module?from?the?PIL?package. from?PIL?import?Image #?`g`?is?a?generator?function?that?returns?a?generator?object. from?loguru?import?logger #?Importing?the?os?module. import?os
定義一下該應(yīng)用能夠支持的圖片格式,如有其他圖片格式轉(zhuǎn)換需要可以在此處進(jìn)行擴(kuò)展。
#?A?list?of?image?formats?that?the?program?will?support. images?=?['jpeg',?'jpg',?'bmp',?'png']
這里首先開發(fā)一個(gè)單個(gè)圖片格式轉(zhuǎn)換的函數(shù)transImage,最后在批量處理中調(diào)用該函數(shù)即可完成批量操作。
def?transImage(source_image=None,?target_type=None,?target_path=None): ????""" ????This?function?takes?an?image?and?converts?it?to?a?different?file?type ????:param?source_image:?The?image?you?want?to?convert ????:param?target_type:?The?type?of?image?you?want?to?convert?to ????""" ????if?source_image?is?None?or?target_type?is?None: ????????logger.error('源圖片路徑或目標(biāo)格式不能為空!') ????????return ????try: ????????img?=?Image.open(source_image) ????????base_name?=?os.path.basename(source_image) ????????target_image_path?=?os.path.join(target_path,?base_name.split('.')[0]?+?'.'?+?target_type) ????????img.save(target_image_path) ????????logger.info('當(dāng)前源圖片:{}格式轉(zhuǎn)換完成!'.format(source_image)) ????except: ????????logger.error('當(dāng)前源圖片:{}格式轉(zhuǎn)換發(fā)生異常!'.format(source_image))
然后,開發(fā)一個(gè)批量圖片處理處理的函數(shù)main_batch,用于直接讀取某個(gè)文件夾下面的所有圖片執(zhí)行格式轉(zhuǎn)換。
def?main_batch(source_path=None,?target_type=None): ????""" ????This?function?takes?an?image?and?converts?it?to?a?different?file?type ????:param?source_image:?The?image?you?want?to?convert ????:param?target_type:?The?type?of?image?you?want?to?convert?to ????:return:?the?image?that?was?converted?to?a?different?file?type. ????""" ????if?source_path?is?None?or?target_type?is?None: ????????logger.info('源圖片批量文件夾路徑或目標(biāo)格式不能為空!') ????????return ????try: ????????for?file_name?in?os.listdir(source_path): ????????????source_file_type?=?file_name.split('.')[1] ????????????if?source_file_type?in?images?and?target_type?in?images: ????????????????transImage(os.path.join(source_path,?file_name),?target_type,?source_path) ????????????else: ????????????????logger.error('圖片格式不符合要求,請(qǐng)自行擴(kuò)展!') ????except: ????????logger.error('批量圖片格式轉(zhuǎn)換失敗,請(qǐng)檢查參數(shù)是否設(shè)置正確!') #?Calling?the?main_batch?function?with?the?source_path?and?target_type?arguments. main_batch(source_path='D:/test-data-work',?target_type='png')
python+win32com輕松完成批量Excel文件的加密!
在辦公的過(guò)程中面對(duì)一些重要數(shù)據(jù)的加密通常都能夠借助wps/office來(lái)完成,但是面對(duì)批量處理的時(shí)候還是有些麻煩。
下文主要說(shuō)明如何使用python的三方模塊完成對(duì)Excel文件的批量加密操作。
其中主要使用到的三方模塊就是win32com,這里需要注意的是在安裝該庫(kù)的時(shí)候?qū)?yīng)的名稱是pywin32。
pip?install?pywin32
接下來(lái),將需要的幾個(gè)python模塊都導(dǎo)入到代碼塊中進(jìn)行后續(xù)的業(yè)務(wù)開發(fā)。
#?It's?importing?the?win32com.client?module. import?win32com.client #?Importing?the?os?module. import?os from?loguru?import?logger
然后,開發(fā)一個(gè)單個(gè)文件的加密函數(shù)excel_set_password_one完成文件加密操作。
def?excel_set_password_one(source_file_path,?target_file_path,?password): ????""" ????It?takes?an?Excel?file,?sets?a?password?on?it,?and?saves?it?to?a?new?file. ????:param?source_file_path:?The?path?to?the?Excel?file?you?want?to?set?a?password?on ????:param?target_file_path:?The?path?to?the?file?you?want?to?save?the?password-protected?file?to ????:param?password:?the?password?you?want?to?set?for?the?excel?file ????""" ????excel?=?win32com.client.Dispatch("Excel.Application") ????#?It's?opening?the?Excel?file. ????wb?=?excel.Workbooks.Open(source_file_path,?False,?False,?None,?'') ????#?It's?turning?off?the?alert?that?pops?up?when?you?try?to?save?a?file?with?a?password. ????excel.DisplayAlerts?=?False ????#?It's?saving?the?file?to?a?new?file?with?a?password. ????wb.SaveAs(target_file_path,?None,?password,?'') ????#?It's?closing?the?Excel?application. ????excel.Quit()
單個(gè)excel文件加密過(guò)程開發(fā)完成之后,需要再創(chuàng)建一個(gè)函數(shù)batch_main可以完成批量執(zhí)行每個(gè)excel文件加密的操作。
def?batch_main(batch_dir,?password): ????""" ????This?function?takes?a?directory?of?files?and?a?password,?and?then?encrypts?all?the?files?in?the?directory?with?the ????password ????:param?batch_dir:?The?directory?where?the?batch?files?are?located ????:param?password:?The?password?to?use?for?the?encrypted?zip?file ????""" ????logger.info('批量處理Excel文件加密過(guò)程開始!') ????if?batch_dir?is?None?or?batch_dir.strip()?==?'': ????????logger.debug('批量處理的文件路徑不能為空!') ????????return ????if?password?is?None?or?password.strip()?==?'': ????????logger.debug('批量處理的文件加密路徑不能為空!') ????????return ????for?file_name?in?os.listdir(batch_dir): ????????if?file_name.__contains__('xlsx'): ????????????source_file_path?=?os.path.join(batch_dir,?file_name) ????????????target_file_path?=?os.path.join(batch_dir,?file_name.split('.')[0]?+?'_已加密.xlsx') ????????????excel_set_password_one(source_file_path,?target_file_path,?password) ????logger.info('批量處理Excel文件加密過(guò)程完成!')
最后一步,使用main函數(shù)調(diào)用批量文件加密的batch_main函數(shù)即可完成對(duì)所有該文件夾下面的文件加密。
if?__name__?==?'__main__': ????batch_main('D:/test-data-work',?'123456') D:\Python\Python311\python.exe?D:\pycharm-projects\the-public\test020\test7.py 2023-01-19?10:35:36.799?|?INFO?????|?__main__:batch_main:64?-?批量處理Excel文件加密過(guò)程開始! 2023-01-19?10:35:40.529?|?INFO?????|?__main__:batch_main:77?-?批量處理Excel文件加密過(guò)程完成!
到此這篇關(guān)于使用Python自制一個(gè)回收站清理器的文章就介紹到這了,更多相關(guān)Python回收站清理器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pytorch 的損失函數(shù)Loss function使用詳解
今天小編就為大家分享一篇Pytorch 的損失函數(shù)Loss function使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Python實(shí)現(xiàn)朗讀在線音頻和本地音頻
在日常的Python軟件開發(fā)中,我們經(jīng)常會(huì)遇到一個(gè)非常重要的功能需求——讓程序能夠讀取并顯示文本內(nèi)容,下面我們就來(lái)學(xué)習(xí)一下Python實(shí)現(xiàn)朗讀音頻的具體操作吧2024-03-03Python實(shí)現(xiàn)刪除列表中滿足一定條件的元素示例
這篇文章主要介紹了Python實(shí)現(xiàn)刪除列表中滿足一定條件的元素,結(jié)合具體實(shí)例形式對(duì)比分析了Python針對(duì)列表元素的遍歷、復(fù)制、刪除等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06python 基于PYMYSQL使用MYSQL數(shù)據(jù)庫(kù)
這篇文章主要介紹了python 基于PYMYSQL使用MYSQL數(shù)據(jù)庫(kù)的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12python yield和Generator函數(shù)用法詳解
這篇文章主要介紹了python yield和Generator函數(shù)用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02