【问题标题】:fnmatch.fnmatch use a pattern to exclude sub directoriesfnmatch.fnmatch 使用模式排除子目录
【发布时间】:2021-06-10 08:00:10
【问题描述】:

我有这样的目录结构

rootFolder/
    - some.jar
    - another.jar
    subDirectory/
                -some1.jar

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

我也尝试过以下模式,但我试图在没有指定模式中子目录的名称的情况下这样做。请参阅 here 并指定目录名称。

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

有什么建议吗?

背景

我正在尝试编写一个用于通过 az cli 上传的通用脚本;我正在使用的函数是upload-batch,它在内部使用 fnmatch,我只能控制使用--pattern 标志传递的模式。见here.

正在使用以下命令:

az storage file upload-batch  --account-name myaccountname --destination dest-directory  --destination-path test/ --source rootFolder --pattern "*.jar" --dryrun

【问题讨论】:

  • 在您如何为fnmatch 创建输入或您希望最终实现什么的问题中添加一些背景信息可能有助于提供适合您的用例的答案。
  • 我也提供了一些背景
  • 那么您真正的问题不是如何使用fnmatch 匹配文件名,而是如何使用文档为fnmatch 引用--pattern 的背景信息指定az storage blob upload-batch... 的参数选项。请在您的问题中显示您想要使用的命令,即使是非工作模式。也许您可以将源目录指定为./root 并使用模式./*.jar/path/to/root/path/to/root/*.jar? (我没有可用于测试的 ac 程序。)
  • 我添加了一个我正在使用的示例命令,虽然我不确定这是否可以通过指定一个模式来实现,因为如果你查看第二个示例,至少需要另一个条件来排除 @987654337 @ 或者如果我们可以将 '.jar' 和 ' */* ' 组合成一个可以解决问题的模式。
  • 您的示例命令似乎与示例目录结构不匹配。当您尝试模式"./*.jar""sourceFolder/*.jar" 时会发生什么?

标签: python azure-storage wildcard azure-cli fnmatch


【解决方案1】:

此答案基于原始问题,该问题似乎是XY problem,因为它询问了使用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, '*/*'))

【讨论】:

  • 我已经提供了一些背景信息来说明我为什么要使用 fnmatch 并且我只能控制模式。
猜你喜欢
  • 2012-11-07
  • 2020-10-18
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
  • 2017-07-08
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多