Python递归查找文件并移动到一个目标目录

发布于 2024-12-04 21:29:21 字数 1464 浏览 0 评论 0原文

该脚本应递归地遍历 rootpath 目录并查找所有具有 *.mp4 扩展名的文件。打印具有目录结构的文件列表。然后将文件移动到 destDir 目录。我遇到的问题是尝试将文件移动到新目录时。只有 rootPath 目录中的文件才会移动到新目标。 rootPath下子目录中的文件导致错误:

/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 <module>
    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'    
############## here is the script
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)

The script should recursively go through the rootpath directory and find all files with *.mp4 extension. Print the list of files with the directory structure. Then move the files to the destDir directory. The problem I run into is when trying to move the files to the new directory. Only files in the rootPath directory will be moved to the new destination. Files in subdirectories under rootPath causes errors:

/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 <module>
    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'    

############## here is the script

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难得心□动 2024-12-11 21:29:21

恭喜!您已经找到了os.path.join()。您甚至可以在 print 调用中使用它。因此,您只需将其与 move() 一起使用即可:(

shutil.move(os.path.join(root, filename), os.path.join(destDir, filename))

但请注意不要覆盖 destDir 中的任何内容。)

Congratulations! You have already found os.path.join(). You even use it, on your print call. So you only have to use it with move():

shutil.move(os.path.join(root, filename), os.path.join(destDir, filename))

(But take care not to overwrite anything in destDir.)

恋竹姑娘 2024-12-11 21:29:21

将最后一行中的 root + filename 更改为 os.path.join(root, filename) (如前面两行所示)?

Change the root + filename in the last line to os.path.join(root, filename) (as seen two lines earlier)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文