Is there a way to open a file with the open()
without knowing its full name?
The linux shell provides an easy way to do that (in some sense), by accepting regular expressions as input.
For example, if you have a folder containing the files:
a.out file1 file2 file3 file4 file.txt test
and you want to list only the files with the prefix file
you can do so by:
$ ls file*
file1 file2 file3 file4 file.txt
Or:
$ ls file[1-9]
file1 file2 file3 file4
To list only numbered files and so on...
I need to open the same file whenever my program launches.
The problem is, the file it needs to open is of the form: X*Y
, meaning it starts with an X
and ends with Y
, but it could be anything in between.
For example, it could be X-Toshiba_12.45y9-Y
, or it might be X-Dell-5.44s-Y
.
I want to be able to open this file without having to consider the model.
The file may reside with some other files in that folder, but the X
prefix and Y
postfix are unique.
I could iterate the files in that folder and try to find my file by matching strings, but I'd rather avoid it.
Is there a way to provide open()
with a regular expression somehow?