想清理一下文件,将桌面上的文件清理到指定的文件夹下。
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会报错。