I need to read a .csv which name is changing over time. For intance, yesterday it was '58724_route_48878.csv' and today its name is '32840_route_41124.csv'.
It always keeps in the name 'route' but numbers before and after that word changes every day. Is there a way to read the file without changing the file name detailed in the script daily?
Thanks in advance!
CodePudding user response:
If you don't have any way to predict how the numbers change in the file name, then your best bet is to use glob
, a Python library that lets you list files in a directory that match a certain wildcard pattern. glob
is documented at docs.python.org
import glob
file_path = glob.glob('*_route_*.csv')[0] # use [0] to pick the first matching file
Note that the sample code above picks the first matching file in the current directory. If there are going to be multiple files that match that pattern, you'll have to come up with more logic to pick the correct one.