Skip to main content
 首页 » 编程设计

python之仅 os.walk .jpg

2025年05月04日17虾米姐

我只想要以 .jpg 或 .pdf 结尾的文件名。如何限制文件名搜索?

import os 
from subprocess import call 
 
for dirname, dirnames, filenames in os.walk('.'): 
    for filename in filenames: 
        jpg = os.path.join(dirname, filename) 
        call(["./curl_recognize.sh", jpg, jpg+".txt", "-f txt"]) 

请您参考如下方法:

import fnmatch 
import os 
 
for file in os.listdir('.'): 
    if fnmatch.fnmatch(file, '*.txt'): 
        print file 

fnmatch 优于其他方法,因为它假设 unix 像通配符,包括:

*= 匹配所有内容

?= 匹配任何单个字符

[seq]=匹配seq中的任意字符

[!seq]= 匹配任何不在 seq 中的字符