如何在Python中从子文件夹中复制特定文件到一个新文件夹?

huangapple go评论110阅读模式
英文:

How to copy specific files from the sub-folders to a new folder in python?

问题

我需要修改代码,只复制每个子文件夹中的最后一个图像(在这种情况下是第7个图像)到一个新文件夹中。

英文:

I have a folder with several sub-folders, each containing the same number of files (here it is 7). The code that I use at the present copies all the files from the different sub-folders within a main folder, to another new folder.

  1. import os
  2. import shutil
  3. src = r'C:\Users\datasets\test
    import os
  4. import shutil
  5. src = r'C:\Users\datasets\test\0'
  6. dest = r'C:\Users\datasets\data_new\test\0'
  7. for path, subdirs, files in os.walk(src):
  8. for name in files:
  9. filename = os.path.join(path, name)
  10. shutil.copy2(filename, dest)
  11. '
  12. dest = r'C:\Users\datasets\data_new\test
    import os
  13. import shutil
  14. src = r'C:\Users\datasets\test\0'
  15. dest = r'C:\Users\datasets\data_new\test\0'
  16. for path, subdirs, files in os.walk(src):
  17. for name in files:
  18. filename = os.path.join(path, name)
  19. shutil.copy2(filename, dest)
  20. '
  21. for path, subdirs, files in os.walk(src):
  22. for name in files:
  23. filename = os.path.join(path, name)
  24. shutil.copy2(filename, dest)

I need to modify the code in a way to copy only the last image (i.e. the 7th image in this case) from each sub-folder (windows file arrangement) to a new folder.

答案1

得分: 2

这应该适合您。

  1. import os
  2. import shutil
  3. from glob import glob
  4. src = r'C:\temp\datasets\test
    import os
  5. import shutil
  6. from glob import glob
  7. src = r'C:\temp\datasets\test\0'
  8. dest = r'C:\temp\datasets\data_new\test\0'
  9. for base, dirs, _ in os.walk(src):
  10.     for path in dirs:
  11.         files = sorted(glob(os.path.join(base, path, '*')))
  12.         if len(files) == 0:
  13.             continue
  14.         file = files[-1]
  15.         filename = os.path.join(path, file)
  16.         shutil.copyfile(filename, dest)
  17. '
  18. dest = r'C:\temp\datasets\data_new\test
    import os
  19. import shutil
  20. from glob import glob
  21. src = r'C:\temp\datasets\test\0'
  22. dest = r'C:\temp\datasets\data_new\test\0'
  23. for base, dirs, _ in os.walk(src):
  24.     for path in dirs:
  25.         files = sorted(glob(os.path.join(base, path, '*')))
  26.         if len(files) == 0:
  27.             continue
  28.         file = files[-1]
  29.         filename = os.path.join(path, file)
  30.         shutil.copyfile(filename, dest)
  31. '
  32. for base, dirs, _ in os.walk(src):
  33. for path in dirs:
  34. files = sorted(glob(os.path.join(base, path, '*')))
  35. if len(files) == 0:
  36. continue
  37. file = files[-1]
  38. filename = os.path.join(path, file)
  39. shutil.copyfile(filename, dest)
英文:

This should do it for you.

  1. import os
  2. import shutil
  3. from glob import glob
  4. src = r'C:\temp\datasets\test
    import os
  5. import shutil
  6. from glob import glob
  7. src = r'C:\temp\datasets\test\0'
  8. dest = r'C:\temp\datasets\data_new\test\0'
  9. for base, dirs, _ in os.walk(src):
  10. for path in dirs:
  11. files = sorted(glob(os.path.join(base, path, '*')))
  12. if len(files) == 0:
  13. continue
  14. file = files[-1]
  15. filename = os.path.join(path, file)
  16. shutil.copyfile(filename, dest)
  17. '
  18. dest = r'C:\temp\datasets\data_new\test
    import os
  19. import shutil
  20. from glob import glob
  21. src = r'C:\temp\datasets\test\0'
  22. dest = r'C:\temp\datasets\data_new\test\0'
  23. for base, dirs, _ in os.walk(src):
  24. for path in dirs:
  25. files = sorted(glob(os.path.join(base, path, '*')))
  26. if len(files) == 0:
  27. continue
  28. file = files[-1]
  29. filename = os.path.join(path, file)
  30. shutil.copyfile(filename, dest)
  31. '
  32. for base, dirs, _ in os.walk(src):
  33. for path in dirs:
  34. files = sorted(glob(os.path.join(base, path, '*')))
  35. if len(files) == 0:
  36. continue
  37. file = files[-1]
  38. filename = os.path.join(path, file)
  39. shutil.copyfile(filename, dest)

huangapple
  • 本文由 发表于 2020年1月4日 01:34:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定