Hello i have a problem finding and opening files in a subdirectory.
I have several different files called for example :
mouse_1_animal.txt
mouse_2_animal.txt
mouse_3_animal.txt
so i want to find all these files in the subdirectory of the working dir and open them and do something with there lines. This is my try:
i=1
for path, subdirs, files in os.walk(root) :
for file in files :
if file == "mouse_{0}_animal.txt".format(i) :
#do something
i = i + 1
but apparently it doesn't find all the files so i'm wondering if it's the way i'm using to find the file that is wrong.
The pythonic way:
import glob
for f in glob.glob('./subDir/mouse_*_animal.txt'):
# do_something
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