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:\\华为手机") 

猜你喜欢

转载自www.cnblogs.com/fishenjoy/p/9126938.html