gpt4 book ai didi

python - 仅 os.walk .jpg

转载 作者:太空宇宙 更新时间:2023-11-04 07:10:21 24 4
gpt4 key购买 nike

我只想要以 .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 中的字符

关于python - 仅 os.walk .jpg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13872441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com