I have a file called something like FILE-1.txt or FILE-340.txt. I want to be able to get the number from the file name. I've found that I can use
numbers = re.findall(r'\d+', '%s' %(filename))
to get a list containing the number, and use numbers[0] to get the number itself as a string... But if I know it is just one number, it seems roundabout and unnecessary making a list to get it. Is there another way to do this?
Edit: Thanks! I guess now I have another question. Rather than getting a string, how do I get the integer?
In response to your new question you can cast the
string
to anint
:If you want your program to be effective
use this:
this will work only to the example that you showed
Another way just for fun:
Adding to F.J's comment, if you want an int, you can use:
Use
search
instead offindall
:Alternatively: