首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fnmatch.fnmatch使用模式排除子目录

fnmatch.fnmatch使用模式排除子目录
EN

Stack Overflow用户
提问于 2021-03-12 09:46:15
回答 1查看 302关注 0票数 0

我有这样的目录结构

代码语言:javascript
复制
rootFolder/
    - some.jar
    - another.jar
    subDirectory/
                -some1.jar

我只想获取rootFolder中的文件,而不是subDirectory (some.jar和another.jar)。

我也尝试了下面的模式,但是我尝试这样做,而没有在模式中指定subDirectory的名称。请参阅指定目录名的这里

我也使用过像'*.jar‘这样的模式,但是它也包含了subDirectory文件。

有什么建议吗?

背景

我正在尝试编写一个通过az cli上传的通用脚本;我使用的函数是upload-batch,它在内部使用fnmatch,其中我只能控制使用--pattern标志传递的模式。请参阅这里。

正在使用以下命令:

代码语言:javascript
复制
az storage file upload-batch  --account-name myaccountname --destination dest-directory  --destination-path test/ --source rootFolder --pattern "*.jar" --dryrun
EN

回答 1

Stack Overflow用户

发布于 2021-03-12 14:35:30

这个答案是基于最初的问题,这个问题似乎是一个XY问题,因为它询问如何使用fnmatch来匹配文件名,而不是如何为AZ CLI指定模式。

您可以使用re而不是fnmatch

代码语言:javascript
复制
import re
testdata = ['hello.jar', 'foo.jar', 'test/hello.jar', 'test/another/hello.jar', 'hello.html', 'test/another/hello.jaring']
for val in testdata :
    print(val, bool(re.match(r"[^/]*\.jar$", val)))

版画

代码语言:javascript
复制
hello.jar True                                                                                                                                                                              
foo.jar True                                                                                                                                                                                
test/hello.jar False                                                                                                                                                                        
test/another/hello.jar False                                                                                                                                                                
hello.html False                                                                                                                                                                            
test/another/hello.jaring False                                                                                                                                                             

或者为/添加第二次检查

代码语言:javascript
复制
import fnmatch
pattern = '*.jar'
testdata = ['hello.jar', 'foo.jar', 'test/hello.jar', 'test/another/hello.jar', 'hello.html', 'test/another/hello.jaring']
for val in testdata :
    print(val, fnmatch.fnmatch(val, pattern) and not fnmatch.fnmatch(val, '*/*'))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66597569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档