0

I'm trying to copy certain .JPG files from my photocard to another directory. The files are named IMG_7853.JPG, IMG_7854.JPG, and so on (they range from IMG_0001.JPG to IMG_9999.JPG). If I want to copy all files greater than IMG_7853 what's the best approach in python. The code below works fine for listing all files in the directory, but I wasn't sure how to go about making the comparison based on the partial filename.

#! /bin/python
import re
import os

def copyphoto():
    path="/media/CANON_DC/DCIM"
    for root, dirs, files in os.walk(path):
        for name in files:
            if name.endswith(".JPG"):
                print name
1
  • What you need to do is extract the numeric portion, convert it to an integer, and make the comparison. Commented Aug 26, 2011 at 4:14

3 Answers 3

3

fnmatch does glob-style matching.

Sign up to request clarification or add additional context in comments.

Comments

2
...
   if name.endswith(".JPG") and int( name.split("_")[-1].split(".")[0] ) > 7853 :
...

1 Comment

Thanks. Thought it would be harder than that.
1

I don't know python but you should be able to just compare them. As long as the files are strings and all start with IMG_ with no leading zeros, you should just be able to use IMG_7853.JPG > filename

3 Comments

Sorry - I didn't post in my original question that there are leading zeros for certain filenames. I edited it to reflect this.
@matt_k: Actally, the leading zeros are good. Go ahead and try Tony's suggestion. It will only work because of the leading zeros.
@matt_k Ah okay, Those leading zeros are perfect. When i mentioned leading zero's that would hurt you i meant more of "IMG_700.JPG" compared to "IMG_0705.JPG". In this case, your "IMG_0705.JPG" would actually be less than your "IMG_700.JPG". Since they all have 4 digit numbers though you won't run into this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.