I need to get all text files with numeric names: 1.txt, 2.txt, 13.txt
Is it possible to do with glob
?
import glob
for file in glob.glob('[0-9].txt'):
print(file)
Does not return 13.txt.
And there seems to be no regex's one or more +
operator.
What can I do?
I don't think that glob()
is meant to be that customisable. You might want to try os.listdir()
instead:
import os,re
for f in os.listdir("/path/to/dir"):
if re.match(r"^\d+\.txt$", f):
print(f)
From TFM:
No tilde expansion is done, but *, ?, and character ranges expressed with [] will be correctly matched
So, no, there are no +
operators as in regex. You can use glob
as a first-pass (as in glob('*.txt')
), and filter it further with regex
.
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