简体   繁体   中英

How to copy file based on partial filename in python

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

fnmatch全局样式匹配。

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM