python剪切文件


心血来潮,手机上导出的图片全部按日期放在不同文件夹,很是麻烦,想放在一起方便浏览,手动操作费时费力,想到bat命令,不是很熟,看到python欣喜不已,很是方便

递归遍历文件,剪切出来,删除空文件夹

不足:未考虑各种异常

#剪切文件
import os

def shear_dile(src,dst):
   
    if  os.path.isdir(src):  
       if  not os.listdir(src):  
            os.rmdir(src)  
            print('移除空目录: ' + src) 
       else :
           for d in os.listdir(src):  
                shear_dile(os.path.join(src, d),dst)  
    if os.path.isfile(src): 
          print ("文件剪切:",src)
          fn=os.path.basename(src)
          if not os.path.exists(dst+'./'+fn):
            os.rename(src,dst+'./'+fn)

shear_dile("H:\\手机图片","D:\\华为手机") 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM