python移动多个子文件中的文件到一个文件夹

 1 import os
 2 import os.path
 3 import shutil
 4 
 5 def listDir(dirTemp):
 6     if None == dirTemp:
 7         return
 8     global nameList
 9     if not os.path.exists(dirTemp):
10         print "file or directory isn't exist"
11         return
12 
13     if os.path.isfile(dirTemp):
14         if dirTemp.find('.wmv') > 0: nameList.append(dirTemp)
15         return
16 
17     resultList = os.listdir(dirTemp)
18 
19     for fileOrDir in resultList:
20         listDir(dirTemp + "/" + fileOrDir)
21 
22     return nameList
23 
24 
25 path = r'F:\java web学习'.decode('utf-8')
26 nameList = []
27 list=listDir(path)
28 for item in list:
29     shutil.move(item, path)

     当一个文件夹在的文件不是直接在一个文件夹的,而是在该文件夹的一个个子文件夹中的时候,就用这段代码实现把全部文件移动到指定文件夹。

posted on 2017-02-24 21:48  J.M.Liu  阅读(878)  评论(0编辑  收藏  举报