使用某个文件夹下的所有文件去替换另一个文件夹下及其子文件夹下存在的同名文件(Python实现)...
值此新年即將到來之際,在這獻上今年最后一篇文章.
產生這個需求是在項目的一次圖標替換上,當時給了我一堆新圖標要替換原來的老圖標,可是原來的老圖標分布在某個文件夾下的各個子文件夾下面,而新圖標全是在同一個目錄下的. 手動替換的話,只能是搜索文件名后替換,但是文件很多太麻煩了,沒找到現成的工具可以實現我的需求.于是有了下面這個腳本,正好熟悉下剛剛學會的Python. 如果有人知道有什么工具可以完成的話不妨留言告知:).
下面腳本實現的就是在dest目錄及其子目錄下面,尋找和src目錄下的文件對應的同名文件,如果找到唯一的一個同名文件,用src里面的新文件替換dest里面對應的老文件. 如果沒找到同名或有多個同名的,就忽略.因為這種情況下需要人工接入是否替換,不過這樣工作量已經少了很多了.
?
代碼通過掃描一遍dest目錄及其子目錄,建立了以文件名為索引,文件所在目錄名為鍵值的倒排索引. 然后對src目錄下的每個文件名去剛剛建立的倒排索引中尋找鍵值,如果鍵值中剛好只有一個目錄名,說明找到了對應的唯一同名文件,替換之.其他的忽略.
代碼如下:
?
代碼 #!/usr/bin/env python# coding:UTF-8
import os,sys,shutil
from os import path
def findandreplace(src,dest):
srcfiles = os.listdir(src)
destfilesdict = {}
#處理目標路徑下(含子目錄下)的所有文件
for root,dirs,files in os.walk(dest):
# 記錄每個文件名所在的路徑,多個同名文件則會有多個不同路徑.
# 這樣就生成了文件名到文件所在路徑的一個倒排索引
for onefile in files:
# 若該文件名的鍵值還未創建則先創建
if onefile not in destfilesdict:
destfilesdict[onefile] = []
destfilesdict[onefile] += [root]
multisamename = []; # 存儲目標目錄及其子目錄下有多個同名文件的文件名
for srcfile in srcfiles:
fullsrcfile = os.path.join(src,srcfile)
if os.path.isfile(fullsrcfile):
if srcfile in destfilesdict:
if len(destfilesdict[srcfile])>1:
multisamename += [srcfile]
else:
# 有且只有唯一的一個同名文件,那么肯定是要替換的那個
shutil.copy(fullsrcfile,destfilesdict[srcfile][0]+'/'+srcfile)
print srcfile + ' replace success.'
print 'following files has more than one in dest directory, replace skipped.'
print '\n'.join(multisamename);
if __name__ == "__main__":
args = sys.argv
if len(args) > 2:
src = args[1]
dest = args[2]
print "all files under the "+dest+\
"(including the subdirectory) will be replaced by the files under " +\
src+" where they have the same name."
if raw_input('Sure(y/n?): ') == 'y':
findandreplace(src,dest)
else:
print "Not enough arguments!"
?
測試代碼如下:
代碼 #!/usr/bin/env python
# coding:UTF-8
import os,sys
import shutil
import findandreplace
rootdir = 'd:/test/'
testsrcdir = 'testsrc'
testdestdir = 'testdest'
testfile = { 'notexist' : ['001','002','003','004','005'], # 替換目標不存在的文件
'onlyone' : ['101','102','103','104','105'], # 有唯一對應的文件存在的
'morethanone':['201','202','203','204','205']} # 多于一個同名存在的
testfileext = '.txt'
# clear old test files
shutil.rmtree(os.path.join(rootdir,testsrcdir),True)
shutil.rmtree(os.path.join(rootdir,testdestdir),True)
# generate src files
os.makedirs(os.path.join(rootdir,testsrcdir))
for key,values in testfile.iteritems():
for filestr in values:
srcfile = open(os.path.join(rootdir,testsrcdir,filestr+testfileext),'w')
srcfile.write(filestr+'srcfile')
srcfile.close()
# generate dest files
os.makedirs(os.path.join(rootdir,testdestdir))
for key,values in testfile.iteritems():
if key == 'notexist':
pass
elif key == 'onlyone':
for filestr in values:
newdir = os.path.join(rootdir,testdestdir,filestr)
os.makedirs(newdir)
srcfile = open(os.path.join(newdir,filestr+testfileext),'w')
srcfile.write(filestr+'destfile')
srcfile.close()
elif key=='morethanone':
for filestr in values:
newdir = os.path.join(rootdir,testdestdir,filestr)
os.makedirs(newdir)
srcfile = open(os.path.join(newdir,filestr+testfileext),'w')
srcfile.write(filestr+'destfile')
srcfile.close()
srcfile = open(os.path.join(rootdir,testdestdir,filestr+testfileext),'w')
srcfile.write(filestr+'destfile')
srcfile.close()
findandreplace.findandreplace(os.path.join(rootdir,testsrcdir),os.path.join(rootdir,testdestdir))
?
Python真是提高效率的利器哈.
?
轉載于:https://www.cnblogs.com/absolute8511/archive/2010/12/31/1923415.html
總結
以上是生活随笔為你收集整理的使用某个文件夹下的所有文件去替换另一个文件夹下及其子文件夹下存在的同名文件(Python实现)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Fact Table and Dimen
- 下一篇: python实现自动打电话软件_电销自动