Reputation: 51
I have about 10 minutes of video and just extracted into every frame so I have more than 100,000 of images in my folder and renamed them from 1 to 100,000. Now I want to select 1 of every 30 from 1 to 100,000 images and move them to another folder. For example : 1, 31,61,91,121,151,181 and so on.
This is my code so far:
import os
import shutil
PATH = './Folder1/'
DEST = './Folder2/'
file = 1
for file in os.listdir(PATH):
file = file + 30
shutil.copyfile(PATH, DEST)
But it gave me following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-b08091703697> in <module>
9 # Get a list of files in the current working directory
10 for file in os.listdir(PATH):
---> 11 file = file + 30
12 shutil.copyfile(PATH, DEST)
TypeError: can only concatenate str (not "int") to str
Thanks in advance for any help!
Upvotes: 0
Views: 200
Reputation: 76
Your error said that you need to convert int to str before adding them together. You can use
file = file + str(30)
then improve your original code later on.
Or you can use my idea below.
for idx in range(1, 100000, 30):
shutil.copyfile(PATH + str(idx), DEST)
Upvotes: 2
Reputation: 619
"file" in for file in os.listdir(PATH)
is a string, so file = file + 30
is not valid.
you should try:
import os
import shutil
PATH = './Folder1/'
DEST = './Folder2/'
filenames = os.listdir(PATH)
for i in range(1, len(filenames), 30):
shutil.copyfile(PATH + filenames[i], DEST)
Upvotes: 0
Reputation: 102
Try the following code
PATH = '/Folder1/'
DEST = './Folder2/'
l = os.listdir(PATH)
file = 1
for file in l[::30]:
shutil.copyfile(PATH, DEST)
Upvotes: 0