search

Home  >  Q&A  >  body text

How to find files named with numbers in Python?

There are the following files:
/images/1.png
/images/2.png
/images/10.png
/images/100.png
/images/a .png

How to find and traverse all files named with numbers?
for i in glob.glob(r'/images/[0-9].png'):
This can only match 1.png and 2.png.

滿天的星座滿天的星座2741 days ago842

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-06-28 09:25:17

    result = [i for i in glob.glob('/images/[0-9]*.png') if i.split('.')[0].isdigit()]
    for i in result:
        print(i)

    reply
    0
  • Cancelreply