I'm writing a python script that is supposed to manage my running files. I want to make sure that the source and target folder exist before I run it and I can do this with os.path.exists
. However, I have a set of foldernames runner<i>
. Is there a way to check that there is some folders begining with that name?
For example, if in the path
/path/to/runners
I have at least one folder named runner
:
/path/to/runners/ $ ls file1.txt
file2.txt
folder1
folder2
runner1 runner35
zfolder
Then the result is true. Remove runner1 and runner35 and it will be false.
You could do the following:
import os
if any(x.startswith('runner') for x in os.listdir('/path/to/runners')):
print "At least one entry begins with 'runner'"
That uses the helpful any
function and a generator expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With