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

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

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.

import os
import shutil

src = r'C:\Users\datasets\test
import os
import shutil
src = r'C:\Users\datasets\test\0'
dest = r'C:\Users\datasets\data_new\test\0'
for path, subdirs, files in os.walk(src):
for name in files:
filename = os.path.join(path, name)
shutil.copy2(filename, dest)
' dest = r'C:\Users\datasets\data_new\test
import os
import shutil
src = r'C:\Users\datasets\test\0'
dest = r'C:\Users\datasets\data_new\test\0'
for path, subdirs, files in os.walk(src):
for name in files:
filename = os.path.join(path, name)
shutil.copy2(filename, dest)
' for path, subdirs, files in os.walk(src): for name in files: filename = os.path.join(path, name) 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

这应该适合您。

import os
import shutil
from glob import glob

src = r'C:\temp\datasets\test
import os
import shutil
from glob import glob

src = r'C:\temp\datasets\test\0'
dest = r'C:\temp\datasets\data_new\test\0'

for base, dirs, _ in os.walk(src):
    for path in dirs:
        files = sorted(glob(os.path.join(base, path, '*')))
        if len(files) == 0:
            continue
        file = files[-1]
        filename = os.path.join(path, file)
        shutil.copyfile(filename, dest)
'
dest = r'C:\temp\datasets\data_new\test
import os
import shutil
from glob import glob

src = r'C:\temp\datasets\test\0'
dest = r'C:\temp\datasets\data_new\test\0'

for base, dirs, _ in os.walk(src):
    for path in dirs:
        files = sorted(glob(os.path.join(base, path, '*')))
        if len(files) == 0:
            continue
        file = files[-1]
        filename = os.path.join(path, file)
        shutil.copyfile(filename, dest)
'
for base, dirs, _ in os.walk(src): for path in dirs: files = sorted(glob(os.path.join(base, path, '*'))) if len(files) == 0: continue file = files[-1] filename = os.path.join(path, file) shutil.copyfile(filename, dest)
英文:

This should do it for you.

import os
import shutil
from glob import glob

src = r'C:\temp\datasets\test
import os
import shutil
from glob import glob
src = r'C:\temp\datasets\test\0'
dest = r'C:\temp\datasets\data_new\test\0'
for base, dirs, _ in os.walk(src):
for path in dirs:
files = sorted(glob(os.path.join(base, path, '*')))
if len(files) == 0:
continue
file = files[-1]
filename = os.path.join(path, file)
shutil.copyfile(filename, dest)
' dest = r'C:\temp\datasets\data_new\test
import os
import shutil
from glob import glob
src = r'C:\temp\datasets\test\0'
dest = r'C:\temp\datasets\data_new\test\0'
for base, dirs, _ in os.walk(src):
for path in dirs:
files = sorted(glob(os.path.join(base, path, '*')))
if len(files) == 0:
continue
file = files[-1]
filename = os.path.join(path, file)
shutil.copyfile(filename, dest)
' for base, dirs, _ in os.walk(src): for path in dirs: files = sorted(glob(os.path.join(base, path, '*'))) if len(files) == 0: continue file = files[-1] filename = os.path.join(path, file) 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:

确定