根据扩展名移动文件小程序

一个移动文件的小程序,主要用来对下载的文件分类存放

python 代码
  1. import os  
  2. import sys  
  3. #shutil is a module for hightlevel file operation    
  4. import shutil  
  5.   
  6. dir='D:\\Downloads'   
  7.   
  8. filetype=raw_input('input the ext of the type you want to sort:')   
  9. destdir=raw_input('where you want to put these %s files to:'%filetype)   
  10. destdir.strip()   
  11.   
  12. if not os.path.exists(destdir):   
  13.     choice=raw_input( '%s does not exist,do you want to create it?[y/n]'%destdir)   
  14.     if choice.strip()=='y':   
  15.         try:   
  16.             os.mkdir(destdir)   
  17.         except:   
  18.             raw_input('failed to creat %s'%destdir)   
  19.             sys.exit()   
  20.     else:   
  21.         raw_input('you choose no,and we cant go on ,good bye!')   
  22.         sys.exit()   
  23. filelist=os.listdir(dir)   
  24.   
  25. for i in filelist:   
  26.     #os.path.splitext() split a the name into file and ext and return as a tuple   
  27.     if os.path.splitext(i)[1][1:] in filetype:   
  28.         command='move %s %s'%(i,destdir)   
  29.         try:   
  30.             #we convert the string to a raw string    
  31.             shutil.move(r'%s'%i,r'%s'%destdir)   
  32.             print 'successfully move %s to %s\n'%(i,destdir)   
  33.         except:   
  34.             print 'error moving %s'%i   
  35.             continue  
  36. raw_input('Done')  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值