Search code examples
pythonlinuxglob

Python glob module is not working with multi selection?


In linux, my folder looks like that:

src
├── app
│   ├── app.component.css
│   ├── app.component.html
│   ├── app.component.spec.ts
│   ├── app.component.ts
│   ├── app.module.ts
│   ├── app-routing.module.ts
│   ├── components
│   │   ├── catalog-main
│   │   │   ├── catalog-main.component.css
│   │   │   ├── catalog-main.component.html
│   │   │   ├── catalog-main.component.spec.ts
│   │   │   └── catalog-main.component.ts
│   │   ├── catalog-sidebar
│   │   │   ├── catalog-sidebar.component.css
│   │   │   ├── catalog-sidebar.component.html
│   │   │   ├── catalog-sidebar.component.spec.ts
│   │   │   └── catalog-sidebar.component.ts
│   │   └── top-bar
│   │       ├── top-bar.component.css
│   │       ├── top-bar.component_handy.css
│   │       ├── top-bar.component_handy.html
│   │       ├── top-bar.component.html
│   │       ├── top-bar.component.spec.ts
│   │       └── top-bar.component.ts
│   ├── icons-provider.module.ts
│   ├── pages
│   │   ├── login
│   │   │   ├── login.component.css
│   │   │   ├── login.component.html
│   │   │   ├── login.component.spec.ts
│   │   │   ├── login.component.ts
│   │   │   ├── login.module.ts
│   │   │   └── login-routing.module.ts
│   │   ├── monitor
│   │   │   ├── monitor.component.css
│   │   │   ├── monitor.component.html
│   │   │   ├── monitor.component.spec.ts
│   │   │   ├── monitor.component.ts
│   │   │   ├── monitor.module.ts
│   │   │   └── monitor-routing.module.ts
│   │   ├── monitor2
│   │   │   └── monitor2.module.ts
│   │   └── welcome
│   │       ├── welcome.component.css
│   │       ├── welcome.component.html
│   │       ├── welcome.component.ts
│   │       ├── welcome.module.ts
│   │       └── welcome-routing.module.ts
│   ├── reducers
│   │   └── index.ts
│   └── store
│       ├── catalog.actions.ts
│       ├── catalog.reducer.ts
│       └── userinfo.ts
├── assets
├── environments
│   ├── environment.prod.ts
│   └── environment.ts
├── favicon.ico
├── index.html
├── main.ts
├── polyfills.ts
├── styles.css
├── test.ts
└── theme.less

I use this command output 29 files:ll src/**/*.ts | wc -l
I use this command output 32 files:ll src/{**,}/*.ts | wc -l, new files are src/main.ts, src/test.ts, src/polyfills.ts, they are direct in src folder

But in python3,
I use glob.glob("src/**/*.ts") output all 32 files,
I use glob.glob("src/{**,}/*.ts") output all 0 files

So, which is standard glob syntax?


Solution

  • which is standard glob syntax?

    Because you tagged linux I assume you use bash. Bash has nostandard (ie. it's not in posix) extension not related to globbing brace expansion.

    The:

    ll src/{**,}/*.ts | wc -l
    

    is first expanded to:

    ll src/**/*.ts src/*.ts | wc -l
    

    by bash when doing brace expansion. Then next happens globbing expansion. Python does not do any shell expansions when parsing glob arguments, so python searches for files/directories named { and ending with , and } taken literally as characters - as it finds none, the count is 0.

    So you are not comparing globbing, you are comparing globbing with shell expansion with only globbing. You can compare your commands with bash with brace expansions disabled set +B or use another shell without brace expansions, like busyboxs.