Reputation: 27
I want to find top-level folders that have only digits in names. F.e. we have such folder structure
.
├── Folder1
| ├── some.file
├── 111
| ├── some.folder
| ├── some.file
| ├── some.file
| ├── some.file-2
├── 555
| ├── some.folder
| ├── some.file
Expected results: found folders '111' and '555'
Here is my code:
import os
main_path = 'C:\\Users'
top_folders_list = next(os.walk(main_path))[1]
condition = '111'
if condition in top_folders_list:
...do_something...
Code works but (of cource) only for folder '111'. Which condition should I use for matching '111', '555' and all other top-level folders that have only digits in names?
Upvotes: 0
Views: 1120
Reputation: 1061
Use isnumeric()
on string object
for fold in fold_lst:
if fold.isnumeric():
print(fold)
Upvotes: 5
Reputation: 158
You could use a regex to filter the condition:
import re
folders = ("123451","8971231")
for folder in folders:
x = re.findall("\\d", folder)
result = "".join(x)
print(result)
And that should give you the desired effect.
Upvotes: 0