1

I want to read the one and only csv file in subfolder foo, using pahtlib, without having to specify the name explicitly. This way, the code still works if the filename changes, but the precondition that there is one and only one csv file in foo is still valid.

Right now I do

from pathlib import Path

foo_dir = Path.cwd() / 'foo'
for file in foo_dir.glob('*'):
    if file.is_file() & (file.suffix = '.csv'):
        csv_file = file
        break 

which is...eh, it works, but it's a bit wordy. Is there anything simpler I could do, without sacrificing readability?

PS how can I modify the above code so that in case there's no csv file in foo, it throws an error?

DeltaIV
  • 4,773
  • 12
  • 39
  • 86
  • 1
    Does this answer your question? [Find all files in a directory with extension .txt in Python](https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python). Specifically, [this answer](https://stackoverflow.com/a/43669828/6045800) – Tomerikoo Apr 18 '21 at 15:07

2 Answers2

2

pathlib.Path.glob is a generator that yields pathlib.Path objects, so you can invoke next on it to consume just the first path-object from the generator. You can supply an optional argument to next to prevent a StopIteration exception from being thrown when no CSV file is present (in this case, next returns None).

from pathlib import Path

if (path := next(Path("dir/to/file").glob("*.csv"), None)) is None:
    print("No .csv file present!")
else:
    print(path)

OR

from pathlib import Path

try:
    path = next(Path("dir/to/file").glob("*.csv"))
except StopIteration:
    raise RuntimeError("No .csv file present!")
Paul M.
  • 10,481
  • 2
  • 9
  • 15
  • oooo I like this - could you please also add the `try-except` alternative? ? I guess something like `try: csv_file = next(foo_dir.glob('*.csv')) except StopIteration: raise RuntimeError('No csv file found')` – DeltaIV Apr 18 '21 at 13:29
  • 1
    @DeltaIV Right, that is the alternative. I'll add it. – Paul M. Apr 18 '21 at 14:58
1

I would use something like this

from glob import glob

try:
    csv_file = glob('foo/*.csv')[0]
except IndexError:
    raise RuntimeError('No csv file found')
Ron Serruya
  • 3,988
  • 1
  • 16
  • 26