I know the format of a file that will be dropped into a folder in windows
.
The contains the filename and then the date with a timestamp such that the file looks like this:
temp_path = 'H:\\Temp\\file_name_' yyyymmddhhmmss '.txt'
Given that i know the date yyyymmdd
but i do not know the time hhmmss
, i replace the time part with a wild card using the below code.
import datetime as dt
# todays date in yyyymmdd format
today = dt.datetime.today()
today_yyyymmdd = today.strftime('%Y%m%d')
# now the file
temp_path = 'H:\\Temp\\test_file_' today_yyyymmdd '*.txt'
print(temp_path)
with open(temp_path, 'r') as f:
data = f.read()
print(data)
The code works if i remove the wildcard *
from in front of the .txt
, but fails with it in place.
However, how can open the file with a wildcard ?
CodePudding user response:
You need to use glob module
import glob
temp_path = glob.glob('file_' today_yyyymmdd '*.txt')[0]