利用Python实现批量移除Mac下的指定文件或者文件夹

有时候Apple Script包装的shell脚本生成的应用程序,会出现在某些机器上无法响应的问题,由于看不到出问题时的环境,所以需要找替换方法,方便用户傻瓜式的一键运行。基于此,考虑用以前用过的Python来简单替代下shell的执行。

目前主要考虑批量移除Mac下的指定文件或者文件夹:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import os,sys
import shutil


applications = "/Applications/"
path_user_app_support = os.path.expandvars("$HOME")+"/Library/Application Support/"
path_user_library = os.path.expandvars("$HOME")+"/Library/"
path_user_launchagents = os.path.expandvars("$HOME")+"/Library/LaunchAgents/"
path_user_launchDaemons = os.path.expandvars("$HOME")+"/Library/LaunchDaemons/"

#/System directory
#path_app_support = "/Library/Application Support/"
#path_library = "/Library/"
#path_launchagents = "/Library/LaunchAgents/"
#path_launchDaemons = "/Library/LaunchDaemons/"


#/Applications
apps = ['Advanced Mac Cleaner.app', 'Mac Adware Cleaner.app']

# ~/Library/Application Support/
directory = ['awc', 'amc', 'MCR']

#~/Library/
library = ['hipradc', 'hipramc']

#~/Library/LaunchAgents
launchagent = ['com.adwarecleaner.hlprawc.plist', 'com.pcv.hlpramcn.plist', 'com.techyutils.mchlpr.plist']

application_rm = [applications + file_name for file_name in apps ]
app_support_rm = [path_user_app_support + file_name for file_name in directory]
user_library_rm = [path_user_library + file_name for file_name in library]
user_launchagent_rm = [path_user_launchagents + file_name for file_name in launchagent]




for app in application_rm:
    if os.path.isdir(app):
        shutil.rmtree(app)
        print("Removed {}".format(app))
    else:
        if os.path.isfile(app):
        	os.unlink(app)
        	print('{} is a file, has been removed '.format(app))
        else:
            print("Cannot find {}! Nothing has been removed ".format(app))

for appsupport in app_support_rm:
    if os.path.isdir(appsupport):
        shutil.rmtree(appsupport)
        print("Removed {}".format(appsupport))
    else:
        if os.path.isfile(appsupport):
        	os.unlink(appsupport)
         	print('{} is a file, has been removed '.format(appsupport))
        else:
            print("Cannot find {}! Nothing has been removed ".format(appsupport))

for userlibrary in user_library_rm:
    if os.path.isdir(userlibrary):
        shutil.rmtree(userlibrary)
        print("Removed {}".format(userlibrary))
    else:
        if os.path.isfile(userlibrary):
        	os.unlink(userlibrary)
         	print('{} is a file, has been removed '.format(userlibrary))
        else:
            print("Cannot find {}! Nothing has been removed ".format(userlibrary))


for userlaunchagent in user_launchagent_rm:
    if os.path.isdir(userlaunchagent):
        shutil.rmtree(userlaunchagent)
        print("Removed {}".format(userlaunchagent))
    else:
        if os.path.isfile(userlaunchagent):
        	os.unlink(userlaunchagent)
         	print('{} is a file, has been removed '.format(userlaunchagent))
        else:
            print("Cannot find {}! Nothing has been removed ".format(userlaunchagent))

运行结果如下,每个模块均能正常移除预先定义的文件或文件夹。

julius-iMac-4:sysman julius$ python remove_file.py
Cannot find /Applications/Advanced Mac Cleaner.app! Nothing has been removed 
Cannot find /Applications/Mac Adware Cleaner.app! Nothing has been removed 
Cannot find /Users/julius/Library/Application Support/awc! Nothing has been removed 
removed /Users/julius/Library/Application Support/amc
Cannot find /Users/julius/Library/Application Support/MCR! Nothing has been removed 
Cannot find /Users/julius/Library/hipradc! Nothing has been removed 
Cannot find /Users/julius/Library/hipramc! Nothing has been removed 
Cannot find /Users/julius/Library/LaunchAgents/com.adwarecleaner.hlprawc.plist! Nothing has been removed 
Cannot find /Users/julius/Library/LaunchAgents/com.pcv.hlpramcn.plist! Nothing has been removed 
Cannot find /Users/julius/Library/LaunchAgents/com.techyutils.mchlpr.plist! Nothing has been removed 

如上所示,对基本的主要目录移除功能是实现了,但是在对root权限文件移除上会失败,再次就是如果要用模糊匹配的删除,还得添加正则表达包装,综合下来还是shell用起来方便快捷。

作为一种备选方法使用吧!

发布了192 篇原创文章 · 获赞 29 · 访问量 45万+

猜你喜欢

转载自blog.csdn.net/julius_lee/article/details/90753840