简体   繁体   English

如何在python中基于部分文件名复制文件

[英]How to copy file based on partial filename in python

I'm trying to copy certain .JPG files from my photocard to another directory. 我正在尝试将某些.JPG文件从我的照片卡复制到另一个目录。 The files are named IMG_7853.JPG, IMG_7854.JPG, and so on (they range from IMG_0001.JPG to IMG_9999.JPG). 这些文件名为IMG_7853.JPG,IMG_7854.JPG,等等(它们的范围从IMG_0001.JPG到IMG_9999.JPG)。 If I want to copy all files greater than IMG_7853 what's the best approach in python. 如果我要复制大于IMG_7853的所有文件,那么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. 我不了解python,但您应该可以比较它们。 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 只要文件是字符串,并且所有文件都以IMG_开头且没有前导零,则您应该只能使用IMG_7853.JPG>文件名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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