-2

I have 10 images in the folder(1.jpg, 2.jpg, 3.jpg, ..., 10.jpg) and I want to copy only 5 images(1.jpg, 2.jpg, 4.jpg, 6.jpg, 8.jpg) into another folder. But I am getting this error.

`
Traceback (most recent call last):
  File "/home/vkaps-lp-101/Desktop/Core Python 
Code/19_Time_lapse/test.py", line 92, 
in <module>
    copytree(d, dst, symlinks = False, ignore = None, 
copy_function = copy2)
  File "/usr/lib/python3.8/shutil.py", line 555, in copytree
    with os.scandir(src) as it:
ValueError: scandir: embedded null character in path
`


`
import os
import numpy as np
import shutil
from PIL import Image
import numpy as geek
arr = os.listdir('10_f/')
src = '10_f/'
dst = 'folder/'
arr.sort()
# print('arr: ', arr)
b = np.array(arr)
# print('b: ', b)
c = b.reshape(-1, 1)[::2].ravel()
print('c: ', c)
d = c.tobytes()
    
print(len(c))
# for file in d:
#     print(file, end=' ')
    # file = file.tobytes()
# print(type(file))
        
out_arr = geek.array_str(c)
# print ("The string representation of input array: ", out_arr)
# print(type(out_arr))
    
from shutil import copytree, ignore_patterns, copy2
copytree(d, dst, symlinks = False, ignore = None, copy_function = 
copy2)

`

1 Answers1

0

I've amended my answer to perform your exact need of copying only those specific files.

import glob
import os
import shutil

input_dir = 'C:/your/input/folder'
output_dir = 'C:/your/output/folder'

file_ids = [1, 2, 4, 6, 8]

# Grabs all the files that have extension (jpg)
files = glob.glob(os.path.join(input_dir, '*.jpg'))
for f in files:
    # Grabs the filename excluding the directory and the extension
    basename = os.path.splitext(os.path.basename(f))
    # Check that basename to see if it's in the list of 
    # IDs we're interested in transferring
    if int(basename) in file_ids:
        shutil.copy(f, output_dir)
NanoBennett
  • 1,802
  • 1
  • 13
  • 13
  • 2
    This is the correct approach but it does not completely address OP's question. OP want to only copy images with even names. – Tim Aug 26 '22 at 18:36
  • Good point although they have the first image as 1.jpg. So I'm not sure there is any method to the madness. – NanoBennett Aug 26 '22 at 19:10
  • Oh right. May just need to filter by a list of manually provided names. – Tim Aug 27 '22 at 20:06