Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open file in python without file's full name?

Tags:

python

I am trying to execute f = open('filename') in python. However, I dont know the full name of the file. All I know is that it starts with 's12' and ends with '.ka',I know the folder where it's located, and I know it is the only file in that folder that starts and ends with "s12" and ".ka". Is there a way to do this?

like image 410
user1492332 Avatar asked Mar 03 '26 18:03

user1492332


1 Answers

Glob is your friend:

from glob import glob
filename = glob('s12*.ka')[0]

Careful though, glob returns a list of all files matching this pattern so you might want to assert that you get the file you actually want somehow.

like image 136
Trevor Avatar answered Mar 06 '26 08:03

Trevor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!