Python-移动文件夹内文件到指定文件夹,同名文件覆盖
在更新升级包的时候,文件夹内的so文件的合包逻辑是新版本覆盖旧版本,代码逻辑是先将最初的升级包内so文件复制到目标文件夹中,之后版本的升级包在复制之前先和目标文件夹内的文件比对,如果文件名一样就删除目标文件夹内对应文件,再将新版本的文件移入,具体解释如下:
一、代码
import os
import shutil
filePath = 'C:\\Users\\HU\\Desktop\\UFP'
aimPath = 'C:\\Users\\HU\\Desktop\\aimappcom'
def comp_del(com_filename,orgpath):    
    n = 0
    for fpath,fdir,ffile in os.walk(aimPath): 
        while n < len(ffile):
            if com_filename == ffile[n]:
                (aimPath + '\\' + ffile[n])
                print('update_file--------' + orgpath + '\\' + ffile[n] + '\n')
            n = n + 1
    
def mov_file(ndfile,edfile,fpos):
    shutil.move(ndfile + '\\' + fpos,edfile)
for i,j,k in os.walk(filePath):
    if "appcom" in i:
        filedir = eval(repr(i).replace('\\', '\\\\'))
        pos = 0
        while pos < len(k):
            comp_del(k[pos],filedir)
            mov_file(filedir,aimPath,k[pos])
            pos = pos + 1
            
二、解释
1.os.walk函数
轮巡原文件夹,获取目录及文件名
2.eval(repr(i).replace('\\', '\\\\'))
因为python中‘\’为转义符,所以要用‘\\\\’来表示双斜杠
3.comp_del函数
复制之前先和目标文件夹内的文件比对:if com_filename == ffile[n]
如果文件名一样就删除:os.remove
4.mov_file函数
将新版本的文件移入:mov_file
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号