python递归搜索文件再同步到服务器,Python递归查找文件并移动到一个目标目录

脚本应递归浏览根路径目录并查找所有带* .mp4扩展名的文件。打印目录结构的文件列表。然后将文件移动到destDir目录。我遇到的问题是当试图将文件移动到新目录。只有rootPath目录中的文件将被移至新目标。在ROOTPATH下子目录中的文件会导致错误:Python递归查找文件并移动到一个目标目录

/Volumes/VoigtKampff/Temp/TEST/level01_test.mp4

/Volumes/VoigtKampff/Temp/TEST/Destination/2levelstest02.mp4

Traceback (most recent call last):

File "/Volumes/HomeFolders/idmo04/Desktop/ScriptsLibrary/Python/recursive_find.py", line 14, in

shutil.move(root+filename, destDir+'/'+filename)

File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/shutil.py", line 281, in move

copy2(src, real_dst)

File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/shutil.py", line 110, in copy2

copyfile(src, dst)

File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/shutil.py", line 65, in copyfile

with open(src, 'rb') as fsrc:

IOError: [Errno 2] No such file or directory: '/Volumes/VoigtKampff/Temp/TEST/Destination2levelstest02.mp4' ##############这里是脚本

import fnmatch

import os

import shutil

rootPath = '/Volumes/VoigtKampff/Temp/TEST/'

destDir = '/Volumes/VoigtKampff/Temp/TEST2/'

matches = []

for root, dirnames, filenames in os.walk(rootPath):

for filename in fnmatch.filter(filenames, '*.mp4'):

matches.append(os.path.join(root, filename))

print(os.path.join(root, filename))

shutil.move(root+filename, destDir+'/'+filename)

2011-09-15

JRM