1

Pythoncode:

for name in glob.glob(first_argument):
    print(name)

Bash:

>$ ls
> a.txt b.txt c.txt program.py
>$ ./program.py *.txt
> a.txt

Somehow glob recognises the pattern, but will only find one match. However, If i write:

>$ ./program.py "*.txt"
> a.txt
> b.txt
> c.txt

I dont get it. Try to find answers in documentation, but couldnt manage. Anyone knows why? Thanks

Snusifer
  • 485
  • 3
  • 17
  • In the first case, the shell expands the wildcard and passes all 4 files to your code. In other words, length of `sys.argv` is 4. When you use `first_argument`, you are effectively `glob`ing `a.txt` and hence, the result. However, shell expansion does not occur in the second case and `*.txt` is passed as-is, which is then handled by `glob`. – Anoop R Desai Sep 05 '19 at 13:17

1 Answers1

2

when you run ./program.py *.txt in bash, it automatically expands *.txt to a.txt b.txt c.txt program.py

You then pass them all to your program and run the glob only on the first param a.txt

When you add the " around it you actually pass *.txt as the argument to glob

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
Elad Almos
  • 21
  • 1