user63898
user63898

Reputation: 30885

check if file exsist in directory by partial name and return the directory name if found (glob returns wrong)

i need to return the directory path if by given the partial name of the file is found doing it with python globe method returns wrong this is what i have ./test/my/app/upgrade/sql.upgarage.script.sh

pathx = ./test/my/app/upgrade/
partial_name = "upgarage.script"

 for inner_dir in glob.glob(pathx + '/*' + partial_name  + '*'):
             if not os.path.exists(dir_path):
                print("...error ..") 

what it returns in inner_dir is the full name of the file and the full path instead of the path which in pathx .

now the case is that the file can be also inside :

/test/my/app/upgrade/ver1/a/sql.upgarage.script.sh

and if found it should return :

 /test/my/app/upgrade/ver1/a/

what is wrong in my glob?

Upvotes: 1

Views: 54

Answers (1)

Cireo
Cireo

Reputation: 4427

Use ** + recursive=True and glob will work for this.

https://docs.python.org/3/library/glob.html#glob.glob

glob.glob(pathname, *, recursive=False)

If recursive is true, the pattern “**” will match any files and zero or more directories, subdirectories and symbolic links to directories. If the pattern is followed by an os.sep or os.altsep then files will not match.

Upvotes: 2

Related Questions