Python脚本:清理桌面

想清理一下文件,将桌面上的文件清理到指定的文件夹下。

Python源代码:

#encoding=utf-8
import os
import shutil

DeskTopPath = "C:\\Users\\me\\Desktop\\"
PublicDeskTopPath = "C:\\Users\\public\\Desktop\\"
DesPath = "C:\\Users\\me\\xhDocs\\"


def ClearFolder(top_dir):
    try:
        os.makedirs(DesPath)
    except IOError:
        print ("destination dir exists")

    if os.path.exists(top_dir) == False:
        print ("not exists")
        return
    if os.path.isdir(top_dir) == False:
        print ("not a dir")
        return
    for dir_path,subpaths,files in os.walk(top_dir,False):
        for file in files:
            file_path=os.path.join(dir_path,file)
            if file_path.endswith("py"):
                print("ignore: %s"%file_path)
            elif files == "desktop.ini":
                print("ignore: %s"%file_path)
            else:
                shutil.move(file_path,DesPath+file)
        #这里还需要遍历移动文件夹

#ClearFolder("./")
ClearFolder(DeskTopPath)
#ClearFolder(PublicDeskTopPath)

现在这个代码有两个地方没有达到预期,一个是不能移动文件夹,一个是不能移动公共的桌面图标。公共桌面图标的路径就是PublicDeskTopPath。

我用的系统是win8.1。目前暂时只能通过输入地址进入这个文件夹。如果真的想移动这个文件夹里面的东西的话,Python会报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值