您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“怎么使用Python自制一個回收站清理器”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
經(jīng)常筆記本電腦的回收站存儲了很多的文件,需要打開回收站全部選中進(jìn)行清理。
但是要是做成python自動化,再將其配置成定時任務(wù)就不需要再去手動操作了?;蛘呔褪侵苯与p擊運(yùn)行即可完成所有回收站的文件清理。
由于實現(xiàn)過程需要調(diào)用windows的終端命令,所以需要安裝winshell。然而有的小伙伴沒有安裝pypiwin32就會報錯沒有win32con模塊,因此,新的python環(huán)境安裝這兩個非標(biāo)準(zhǔn)庫就OK了。
pip install winshell pip install pypiwin32
其實真正可以使用到的代碼塊只有一行,就是直接調(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("請按任意鍵關(guān)閉窗口...")
上述的代碼塊已經(jīng)能夠完成功能了,然后直接使用pyinstaller打包成exe,項目內(nèi)容較小這里直接采用單文件方式進(jìn)行打包。
pyinstaller -F -i .\recycle.ico .\recycle.py
程序打包完成后生成recycle.exe,可以修改成任意名稱.exe,雙擊執(zhí)行即可完成回收站清理。
recycle.exe
知識補(bǔ)充
除了上文,小編還給大家整理了用Python實現(xiàn)的其他實用小工具,希望對大家有所幫助
批量圖片格式轉(zhuǎn)換器
經(jīng)常在不同的工作場景中需要使用不同的圖片格式來完成相應(yīng)的操作,因此開發(fā)了這款批量轉(zhuǎn)換圖片格式的操作工具。
下文介紹的圖片轉(zhuǎn)換過程主要包含了四種圖片格式的相互轉(zhuǎn)換,分別是jpeg/jpg/png/bmp,通過獲取Image圖片對象從而保存為任意的圖片格式。
今天使用到的圖片處理庫就是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ā)一個單個圖片格式轉(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ā)一個批量圖片處理處理的函數(shù)main_batch,用于直接讀取某個文件夾下面的所有圖片執(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('圖片格式不符合要求,請自行擴(kuò)展!') except: logger.error('批量圖片格式轉(zhuǎn)換失敗,請檢查參數(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文件的加密!
在辦公的過程中面對一些重要數(shù)據(jù)的加密通常都能夠借助wps/office來完成,但是面對批量處理的時候還是有些麻煩。
下文主要說明如何使用python的三方模塊完成對Excel文件的批量加密操作。
其中主要使用到的三方模塊就是win32com,這里需要注意的是在安裝該庫的時候?qū)?yīng)的名稱是pywin32。
pip install pywin32
接下來,將需要的幾個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ā)一個單個文件的加密函數(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()
單個excel文件加密過程開發(fā)完成之后,需要再創(chuàng)建一個函數(shù)batch_main可以完成批量執(zhí)行每個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文件加密過程開始!') 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文件加密過程完成!')
最后一步,使用main函數(shù)調(diào)用批量文件加密的batch_main函數(shù)即可完成對所有該文件夾下面的文件加密。
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文件加密過程開始! 2023-01-19 10:35:40.529 | INFO | __main__:batch_main:77 - 批量處理Excel文件加密過程完成!
“怎么使用Python自制一個回收站清理器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。