python全盘搜索文件,PYTHON:从数组中搜索文件名,然后重定位文件

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值