我有这样的目录结构
rootFolder/
- some.jar
- another.jar
subDirectory/
-some1.jar我只想获取rootFolder中的文件,而不是subDirectory (some.jar和another.jar)。
我也尝试了下面的模式,但是我尝试这样做,而没有在模式中指定subDirectory的名称。请参阅指定目录名的这里。
我也使用过像'*.jar‘这样的模式,但是它也包含了subDirectory文件。
有什么建议吗?
背景
我正在尝试编写一个通过az cli上传的通用脚本;我使用的函数是upload-batch,它在内部使用fnmatch,其中我只能控制使用--pattern标志传递的模式。请参阅这里。
正在使用以下命令:
az storage file upload-batch --account-name myaccountname --destination dest-directory --destination-path test/ --source rootFolder --pattern "*.jar" --dryrun发布于 2021-03-12 14:35:30
这个答案是基于最初的问题,这个问题似乎是一个XY问题,因为它询问如何使用fnmatch来匹配文件名,而不是如何为AZ CLI指定模式。
您可以使用re而不是fnmatch
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)))版画
hello.jar True
foo.jar True
test/hello.jar False
test/another/hello.jar False
hello.html False
test/another/hello.jaring False 或者为/添加第二次检查
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, '*/*'))https://stackoverflow.com/questions/66597569
复制相似问题