我有一个这样的目录结构
rootFolder/
- some.jar
- another.jar
subDirectory/
-some1.jar
我只想获取根文件夹中的文件,而不是子目录中的文件(一些.jar和另一个.jar)。
我也尝试了以下模式,但我试图在没有指定模式中子目录的名称的情况下执行此操作。请参阅此处并指定目录名称。
我也使用了像"*.jar"这样的模式,但它也包括子目录文件。
有什么建议吗?
背景
我正在尝试编写一个通用脚本以通过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
这个答案是基于原始问题,这似乎是一个 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, '*/*'))