0

I want to create a list of all files in a directory using pathlib's glob. The file names may have an extension (ex.: 'text.txt', 'abc.csv') or not ('text', 'abc'). I do realize that this can be handled by the same solution which is the answer to 'How to glob two patterns with pathlib?' but at the moment I cannot believe that something so simple has no simple solution (though it is difficult to understand why glob doesn't allow a list of patterns).

Edit:

  • The file names are just examples; so filtering for them is not my goal.
  • There is a solution using two calls of glob and adding the results. The question is more specific: is there a simpler solution.
  • using the pattern '**/*' is including all subdirectories, which is not intended
  • using os.listdir only shifts the problem: now you have to filter out the directories.
fotis j
  • 427
  • 1
  • 6
  • 14
  • Does this answer your question? [Python: How to read all files in a directory?](https://stackoverflow.com/questions/26695903/python-how-to-read-all-files-in-a-directory) – Christopher Peisert May 10 '22 at 23:17
  • 1
    What exactly are you trying to do? My first thought is, why can't you use something like `os.listdir` which would read all the files in a directory regardless of extension. – Alec Mather May 10 '22 at 23:30
  • there is no simple solution. Frankly, you may have dictionary with name `text.txt` so it may still need to filter values. `glob` and `listdir` don't care if name is file or directory. Only `os.walk` gives tuple `root, dirs, files` but it automatically runs for all subfolders so you would have to use `break` to exit on first loop. – furas May 11 '22 at 02:35
  • @furas probably you are right. I was hoping I just hadn't found it yet. – fotis j May 11 '22 at 19:43

0 Answers0