Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

os.walk Python can't print all drivers

Im using os module to get all the files in a directory

drives = [ chr(x) + ":\" for x in range(65,91) if os.path.exists(chr(x) + ":\") ]

print(drives)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

prints out

[‘C:\’, ‘D:\’]

then I look for all the files in those disks with

counter = 0
inp = '.png'
thisdir = os.getcwd()
for r, d, f in os.walk(drives[0-1]):
    for file in f:
        filepath = os.path.join(r, file)
        if inp in file:
            counter += 1
            print(os.path.join(r, file))           
print(f"counted {counter} files.")

but I get only D:\ drives ‘.png’ file’s which print’s out 55.000 of pictures location on the drive I cant get c:\ what am I doing here wrong I’m a bit python newbie right now so I don’t know what to do can someone help me please?

>Solution :

To reply to your comment, this is how you’d loop over drive:

counter = 0
inp = '.png'
for drive in drives:
    for r, d, f in os.walk(drive):
        for file in f:
            filepath = os.path.join(r, file)
            if inp in file:
                counter += 1
                print(os.path.join(r, file))           
print(f"counted {counter} files.")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading