2

I have a file whose name will get updated daily, but the name will always match some simple pattern e.g. it will always begin with 'report' e.g. report 1 A 2X.csv

How do I open the file on any given day, not knowing what its name will be but knowing the pattern and knowing there will only ever be one file in the folder that matches?

If I define:

pattern = 'report'

then how do I combine os.listdir('.') and str.startswith(pattern) with open() to achieve this?

Pyderman
  • 14,809
  • 13
  • 61
  • 106

4 Answers4

1

Try this:

from glob import glob
file = open(glob('report *')[0])
Amr Ayman
  • 1,129
  • 1
  • 8
  • 24
1

You can find something with regex like this:

import re
x = re.findall(r'report.*csv', 'any text here report 1 A 2X.csv some more stuff here' )

Output:

['report 1 A 2X.csv']
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48
1

You can take a look at the glob module . It accepts a pattern and returns a list of all files that match the pattern.

Example -

>>> import glob
>>> glob.glob('*.py')
['a.py', 'b.py', 'getpip.py']

You can then iterate over the result of this glob.glob and open each file and do your logic -

Example -

for f in glob.glob('report*'):
    <your logic>

Please note,glob uses shell-style wildcards for pattern (not regular expression) .

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
1

As others have pointed out, re, glob, and fnmatch() are great pattern matching tools. Since your pattern is simple enough, startswith and endswith will suit your needs.

I have a directory with some csv files in it, notably one named report_1_A_2x.csv. We can list all csv files that start with report and end with the suffix .csv. In this case there exists only one that matches, so we open it and then do something:

import os

path = '.' # path to file
pattern = 'report'
suffix = '.csv'

files = [f for f in os.listdir(path) if f.startswith(pattern) and f.endswith(suffix)]
print files # prints: ['report_1_A_2x.csv']

with open(os.path.join(path, files[0]), 'r') as rep:
    # do something ...
Scott
  • 6,089
  • 4
  • 34
  • 51