-1

I am trying to understand how glob works, also how is the star * related to glob and how it works vs. how it works in regex?

For example, I know glob.glob("C:\Users\Me\MyFolder\*.txt") would match and return any files with the extension .txt in the given path but how would that compare to finding text files using Regex? Is one better in performance than the other? higher speed or less computation memory?

Archeologist
  • 169
  • 1
  • 11
  • ...anyhow -- for a question to be on-topic, it needs to be narrow and specific; thus, asking about not just a character, but a specific syntactical use. And almost certainly, there will already be Q&A entries in our database covering any specific usage you might want to ask about. – Charles Duffy Jun 17 '19 at 14:59
  • Hey there! I would recommend doing some python research for [3.7.3](https://docs.python.org/3/library/glob.html) or for [2.7.16](https://docs.python.org/2/library/glob.html) – JROS Jun 17 '19 at 14:59
  • To be fair, the Python documentation doesn't explain the glob patterns, only "according to the rules used by the Unix shell". – mkrieger1 Jun 17 '19 at 15:00
  • ... which will lead to e.g. http://man7.org/linux/man-pages/man7/glob.7.html when searched for. – mkrieger1 Jun 17 '19 at 15:01
  • Sure; *if* that's what the OP wants to know, https://wiki.bash-hackers.org/syntax/pattern may be pertinent, with the understanding that no extensions are covered. However, the body of the question doesn't in any way back up that interpretation; it's only the title that mentions globs at all. – Charles Duffy Jun 17 '19 at 15:01
  • Well, given "I am trying to understand how glob works" I think we can assume that the question is about glob patterns, not `*args`. – mkrieger1 Jun 17 '19 at 15:02
  • Thanks @mkrieger1 for your link. It was helpful! – Archeologist Jun 17 '19 at 15:35
  • @JROS, that edit definitely brings the body in line with the title; I'd want to see the OP approve it as what they meant to ask before accepting an edit that speaks so loudly to a question's intent (and reopening the question, as the flagged duplicates would clearly no longer be suitable). – Charles Duffy Jun 17 '19 at 15:41
  • @CharlesDuffy First, I think I made it very clear in the question that I am new to python, so apologies if my question caused so much inconvenience to you. However, thanks for your help! Second, when I was learning using the function '''glob''' in python, I tried to link it a little bit to the command '''find''' in Ubuntu such that it I can use '''find''' to look for files either with certain names or certain file extentions. Therefore, I just wanted to understand it further along with these symbols related to scripting. – Archeologist Jun 17 '19 at 16:09

1 Answers1

2

If we are talking about a '*' in the pattern, then, typically * just means "match any number of characters" or better "match 0 or more characters", so if we assume we have files in a directory: apple cherry custard green_apple then you can get lists of files for example:

import glob

print("glob.glob('a*') -> {}".format(glob.glob('a*'))) # match starting with 'a'

print("glob.glob('*a*') -> {}".format(glob.glob('*a*'))) # match anything that contains an 'a'

print("glob.glob('apple*') -> {}".format(glob.glob('apple*'))) # match if starts with 'apple'

print("glob.glob('*apple*') -> {}".format(glob.glob('*apple*'))) # match if 'apple' is in the filename

This would return

glob.glob('a*') -> ['apple']
glob.glob('*a*') -> ['apple', 'custard', 'green_apple']
glob.glob('apple*') -> ['apple']
glob.glob('*apple*') -> ['apple', 'green_apple']

This is a very simplistic view of what you can do with glob.glob.

brechmos
  • 1,278
  • 1
  • 11
  • 22