您的位置:首页 > 编程语言 > Python开发

Python应用实例

2013-05-19 16:36 295 查看
学了Python一段时间,想用这门脚本语言写一个脚本,用来处理桌面文件的整理,设计思路及相关资料链接整理如下:

1.读取windows系统注册表文件,获得相关桌面的地址--(http://blog.csdn.net/hcj116/article/details/7747940)

2.获取桌面地址下的文件及文件夹的名称,及文件属性--(http://home.cnblogs.com/group/topic/654.html)

3.获取各文件的扩展名--(http://home.cnblogs.com/group/topic/654.html)

4.获取各文件的修改时间--(http://hi.baidu.com/icyday315/item/5aebb76f501ba300a1cf0fda)

5.根据扩展名对各文件进行归类,包括新建文件夹操作,移动文件操作--(http://l90z11.blog.163.com/blog/static/187389042201312153318389/)

思路大致如上,之后会将各部分参考的网络资料补齐,并给出最终的代码,希望能和大家一起共同学习。截止时间2013.5.22晚。

2013.5.20

OnekeyDesktop.py

#步骤一,由注册表获取桌面地址

import winreg
def GetDesktopPath():
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
return(winreg.QueryValueEx(key, "Desktop")[0])
def GetOtherDesktopPath():
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders")
return(winreg.QueryValueEx(key, "Desktop")[0])


#步骤二,获取桌面文件及文件夹名称列表

import os
def GetFolderAndFileName(DesktopPath):
Folder_File = {}
for d, fd, fl in os.walk(DesktopPath):
Folder_File = {'Path':d,
'Folders':fd,
'Files':fl}
return(Folder_File)


#步骤三,获取文件扩展名,并返回分类字典

def FindSameTypeFile(files):
file_type = []
file_dic = {}
for each_file in files:
temp_type = each_file.split('.')[-1]
if not temp_type in file_type:
file_type.append(temp_type)
#file_dic should be initialized to the list, otherwise the function append could not be used
file_dic[temp_type] = [each_file]
else:
file_dic[temp_type].append(each_file)
return(file_dic)


#步骤四,获取文件修改时间,并返回分类字典

import time#and os
def GetFileMTime(file):
modify_time = time.localtime(os.path.getctime(file))
return(str(modify_time.tm_year) + '-' +
str(modify_time.tm_mon))

def FindSameTimeFile(file_path_head,files):
modify_time = []
file_dic = {}
for each_file in files:
temp_time = GetFileMTime(file_path_head + '\\' + each_file)
if not temp_time in modify_time:
modify_time.append(temp_time)
file_dic[temp_time] = [each_file]
else:
file_dic[temp_time].append(each_file)
return(file_dic)


#步骤五,根据分类字典,创建文件夹并将相关文件归类

import shutil
def OrganizeFile(file_path_head, file_dic):
for each_key in file_dic.keys():
filepath_new = file_path_head + '\\' + each_key + '\\'
if not os.path.exists(filepath_new):
os.mkdir(filepath_new)
for each_file in file_dic[each_key]:
shutil.move(file_path_head + '\\' + each_file, filepath_new)


main.py

import OnekeyDesktop

desktop = OnekeyDesktop.GetDesktopPath()
files = OnekeyDesktop.GetFolderAndFileName(desktop)['Files']
file_dic_type = OnekeyDesktop.FindSameTypeFile(files)
file_dic_time = OnekeyDesktop.FindSameTimeFile(desktop, files)
OnekeyDesktop.OrganizeFile(desktop, file_dic_type)

目前运行main.py后,可以使得桌面按照文件类型归类成相关文件夹,完成一开始预期的“一键桌面清理的目标”,包括一堆的快捷方式,及各种文件,特别适合我这种很久都懒得清理桌面的人,呵呵

可以继续改进的部分:

1.文件类型归类后,根据修改时间,对同一天的文件再归类,函数已写出,但是没有在main.py中实现

2.Windows桌面路径除了当前用户桌面外,还包括公用桌面,部分程序的快捷方式会被放在公用桌面,但读取的路径为相对路径,包含环境变量,如%USERPROFILE%之类,考虑用正则表达式来处理,下篇博文就写关于正则表达式~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息