免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2495 | 回复: 8
打印 上一主题 下一主题

发一个修改收藏夹的py代码,我经常在重装机器后双击下就可以恢复收藏夹内容了。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-05-27 17:00 |只看该作者 |倒序浏览
  1. #coding=utf-8

  2. '''
  3. * 恢复收藏家内容的工具
  4. * 版权所有
  5. * @author      t6760915<t6760915@gmail.com>
  6. * @version     $Id: modifyFavorites.py $
  7. '''

  8. import os
  9. import sys

  10. #定义方法,删除某个目录下所有文件夹和文件
  11. def delAll(parentDir, topDir):
  12.     parentDir = os.path.normpath(parentDir)
  13.     topDir = os.path.normpath(topDir)
  14.     parentDirGbk = parentDir.decode('utf-8', 'ignore').encode('gbk', 'ignore')
  15.     topDirGbk = topDir.decode('utf-8', 'ignore').encode('gbk', 'ignore')

  16.     if not os.path.exists(parentDirGbk):
  17.         return True

  18.     if os.path.isdir(parentDirGbk):
  19.         for subDir in os.listdir(parentDirGbk):
  20.             subDir = subDir.decode('gbk', 'ignore').encode('utf-8', 'ignore')
  21.             subDir = os.path.normpath('%s/%s' % (parentDir, subDir))
  22.             delAll(subDir, topDir)
  23.         
  24.         if topDir == parentDir:
  25.             pass
  26.         else:
  27.             try:
  28.                 os.rmdir(parentDirGbk)
  29.             except:
  30.                 pass
  31.         
  32.     elif os.path.isfile(parentDirGbk):
  33.         try:
  34.             os.unlink(parentDirGbk)
  35.         except:
  36.             pass
  37.     else:
  38.         pass
  39.         

  40. #定义收藏夹位置
  41. #FavoritesDir = 'C:/Documents and Settings/ko/Favorites',ko是你的用户名
  42. FavoritesDir = 'C:/Documents and Settings/ko/Favorites'
  43. delAll(FavoritesDir, FavoritesDir)

  44. #定义收藏夹地址字典
  45. FavoritesSites = []


  46. FavoritesSites.append(['链接', 'http://www.google.com/'])
  47. FavoritesSites.append(['链接', 'http://www.baidu.com/'])
  48. FavoritesSites.append(['链接', 'http://www.verycd.com/'])
  49. FavoritesSites.append(['链接', 'http://www.gougou.com/'])
  50. FavoritesSites.append(['链接', 'http://www.tianya.cn/'])

  51. FavoritesSites.append(['', 'http://www.python.org/ftp/python/'])
  52. FavoritesSites.append(['', 'http://www.djangoproject.com/'])
  53. FavoritesSites.append(['', 'http://www.webpy.org/'])
  54. FavoritesSites.append(['', 'http://bbs.chinaunix.net/'])
  55. FavoritesSites.append(['', 'http://www.javaeye.com/'])
  56. FavoritesSites.append(['', 'http://community.csdn.net/'])
  57. FavoritesSites.append(['', 'http://groups.google.mw/'])
  58. FavoritesSites.append(['', 'http://www.oschina.net/'])

  59. #将定义好的地址写入文件
  60. for site in FavoritesSites:
  61.     dirName,url = site

  62.     if not url:
  63.         continue

  64.     if not url.startswith('http'):
  65.         continue

  66.     if not '/' in url:
  67.         continue

  68.     subDirPath = os.path.normpath(FavoritesDir+'/'+dirName).decode('utf-8', 'ignore').encode('gbk', 'ignore')
  69.    
  70.     if os.path.exists(subDirPath):
  71.         pass
  72.     else:
  73.         try:
  74.             os.makedirs(subDirPath)
  75.         except:
  76.             pass

  77.     urlName = '%s.url' % url.split('//')[1].split('/')[0].replace(':', '-')
  78.     subFilePath = os.path.normpath(FavoritesDir+'/'+dirName+'/'+urlName).decode('utf-8', 'ignore').encode('gbk', 'ignore')
  79.     fileContent = '''[InternetShortcut]\nURL=%s''' % url

  80.     try:
  81.         fp = open(subFilePath, 'w')
  82.         fp.write(fileContent)
  83.         fp.close()
  84.     except:
  85.         pass

  86. print 'modifyFavorites success!'
  87. os.system('pause')
  88. sys.exit(0)
复制代码
modifyFavorites.rar (1.16 KB, 下载次数: 56)

论坛徽章:
0
2 [报告]
发表于 2010-05-27 17:04 |只看该作者
呵呵,现在收藏夹都同步在网上了……

论坛徽章:
0
3 [报告]
发表于 2010-05-28 09:19 |只看该作者
这个不错,顶

论坛徽章:
0
4 [报告]
发表于 2010-05-28 14:29 |只看该作者
Chrome和Firefox的书签有人实现吗?

论坛徽章:
0
5 [报告]
发表于 2010-05-28 14:50 |只看该作者
Chrome和Firefox的书签有人实现吗?
2gua 发表于 2010-05-28 14:29



    这两者用扩展就行吧。自己写有点重复造轮子了。

论坛徽章:
0
6 [报告]
发表于 2010-05-28 16:51 |只看该作者
其实FavoritesDir = 'C:/Documents and Settings/ko' 可以用 os.getenv('USERPROFILE') 代替,这样就增加了通用性了

论坛徽章:
0
7 [报告]
发表于 2010-05-28 16:55 |只看该作者
其实FavoritesDir = 'C:/Documents and Settings/ko' 可以用 os.getenv('USERPROFILE') 代替,这样就增加了 ...
xubizhen 发表于 2010-05-28 16:51



    谢谢提醒,我已经做了修改,我本来是想怎么获取当前操作系统的用户呢,其实你这个更好。

论坛徽章:
0
8 [报告]
发表于 2010-05-28 18:58 |只看该作者
谢谢提醒,我已经做了修改,我本来是想怎么获取当前操作系统的用户呢,其实你这个更好。
t6760915 发表于 2010-05-28 16:55



    其实这个只是提取环境变量的值而已

论坛徽章:
0
9 [报告]
发表于 2010-05-29 09:09 |只看该作者
谢谢提醒,我已经做了修改,我本来是想怎么获取当前操作系统的用户呢,其实你这个更好。
t6760915 发表于 2010-05-28 16:55

win32你可以这样。
import  win32api
print win32api.GetUserName()
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP