Home > Net >  os.walk Python can't print all drivers
os.walk Python can't print all drivers

Time:05-31

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)

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?

CodePudding user response:

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.")
  • Related