0

I have a directory structure like this

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

I only want to get the files in the rootFolder and not the subDirectory (some.jar and another.jar).

I have also tried the following pattern but I was trying to do it without specifying the name of the subDirectory in the pattern. See here with directory name specified.

I have also used pattern like '*.jar' but it also includes the subDirectory file.

Any suggestions?

Background

I am trying to write a generic script for uploading via az cli; the function I am using is upload-batch and it internally uses fnmatch where I only have control of the pattern I pass using the --pattern flag. See here.

Below command is being used:

az storage file upload-batch  --account-name myaccountname --destination dest-directory  --destination-path test/ --source rootFolder --pattern "*.jar" --dryrun
Syed Osama Maruf
  • 1,895
  • 2
  • 20
  • 37
  • Adding some background information to the question how you create the input for `fnmatch` or what you want to achieve at the end might help to provide an answer that fits your use case. – Bodo Mar 12 '21 at 14:37
  • I have provide some background as well – Syed Osama Maruf Mar 15 '21 at 05:53
  • Then your real problem is not how to match file names using `fnmatch` but how to specify the arguments for `az storage blob upload-batch...` with the background information that the documentation refers to `fnmatch` for the `--pattern` option. Please show in your question the command you would like to use, even with a non-working pattern. Maybe you can specify the source directory as `./root` and use a pattern `./*.jar` or `/path/to/root` and `/path/to/root/*.jar`? (I don't have this `ac` program available for testing.) – Bodo Mar 15 '21 at 10:38
  • I have added an example command I am using although I am not sure this would be possible by just specifying a pattern since if you second example is looked at one would atleast need another condition to exclude the `/` or if we can combine the '*.jar' and ' */** ' in one pattern that would do the trick. – Syed Osama Maruf Mar 15 '21 at 12:52
  • Your example command doesn't seem to match the example directory structure. What happens when you try patterns `"./*.jar"` or `"sourceFolder/*.jar"`? – Bodo Mar 15 '21 at 14:12
  • I have updated the command to match the example folder structure – Syed Osama Maruf Mar 16 '21 at 06:32
  • Any results with patterns `"./*.jar"` or `"sourceFolder/*.jar"`? – Bodo Mar 16 '21 at 08:11
  • No I have tried both do not work although the second one you have suggested does work but also matches the nested files see [python compiler](https://onlinegdb.com/SyGo7E1Vd). – Syed Osama Maruf Mar 17 '21 at 07:45
  • Please [edit] your question and add all requested information to the question. Comments might get deleted and you make it harder for others to help you when the information is scattered in comments. Did you try with the actual command or only with your own Python code? I don't know how the pattern is actually applied to the affected files. Is the source code of the CLI available? – Bodo Mar 17 '21 at 09:50

1 Answers1

1

This answer is based on the original question which seems to be an XY problem as it asked about matching file names using fnmatch instead of how to specify a pattern for the AZ CLI.

You could use re instead of 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)))

prints

hello.jar True                                                                                                                                                                              
foo.jar True                                                                                                                                                                                
test/hello.jar False                                                                                                                                                                        
test/another/hello.jar False                                                                                                                                                                
hello.html False                                                                                                                                                                            
test/another/hello.jaring False                                                                                                                                                             

or add a second check for /

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, '*/*'))
Bodo
  • 9,287
  • 1
  • 13
  • 29