Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

open a single file with a wildcard

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'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ?

>Solution :

You need to use glob module

import glob
temp_path = glob.glob('file_' + today_yyyymmdd + '*.txt')[0]

https://docs.python.org/fr/3.6/library/glob.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading