How do I find a filename starting with "dec2file" that has an extension on OSX?
In my case, I have only one .ppt file in the Documents directory. So, the result should be: dec2file.ppt
Here is the code:
my_pathname='Documents'
my_filename='dec2file'
my_glob = "{c}.{ext}".format(c=my_filename, ext='*')
try:
my_filename = str(list(pathlib.Path(my_pathname).glob(my_glob))[0])
except Exception as ex:
print("Error - {d}/{f} - {e}".format(d=my_pathname, f=my_glob, e=str(ex)))
exit(1)
print("Found it - {f}".format(f=my_filename))
Current result:
ERROR - Documents/dec2file.* - list index out of range
How do I get it to find the file and print:
Found it - dec2file.ppt
After creating a folder called
test, and a file inside it calleddec2file.txt, I ran this:And got:
So, I can only conclude there is no folder called
Documentsinside the working directory where your script runs. Try replacingmy_pathnamewith a full path name, or ensure your script runs in the parent directory ofDocuments.You can do this by either changing the working directory of the script from your IDE or on the command line, or by using
os.chdiror something similar to change directory before the relevant part of the script.