I'm new to Python and could really use some help. I have a large collection of images that I'm sorting. I need every 260th image (for example: 0, 260, 520, 780, etc). I then need to relocate those images to a new folder. Here is my code so far:
import os, os.path, sys, shutil
root = '.'
dst = "/Users/Desktop"
print "/// F I N D__A L L__F I L E S __W I T H I N __R A N G E ///"
selectPhotos = range(260, 213921)
print selectPhotos[::260]
print "/// L I S T__O F __A L L __J P E G S ///"
for files in os.listdir("/Users/Desktop/spaceOddy/"):
#if files.endswith(".jpg"):
# print files
if files.startswith(selectPhotos[]):
print files
shutil.move ("files", root)
My code isn't working in two places.
I receive an error that I need to pass a tuple into startswith, which I don't know how to do. I know what a tuple is but in terms of syntax I'm in the dark.
I don't know much about shutil.move. If anyone knows of a better approach I'd appreciate it.
Thanks,
解决方案
To move every nth image file in a directory to another directory:
#!/usr/bin/env python
from __future__ import print_function
import glob
import shutil
import sys
dstdir = "/Users/Desktop"
for file in glob.glob("/Users/Desktop/spaceOddy/*.jpg")[::260]:
try:
shutil.move(file, dstdir)
except EnvironmentError as e:
print("can't move {}, error {}".format(file, e), file=sys.stderr)