Can't print filepath of all files on all drives


My code in order till the line.

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

I see all the extentions of files in a specified disk with this code block

ListFiles = os.walk("d:\\") #normally putting drives here. and getting an error.
SplitTypes = []
for walk_output in ListFiles:
    for file_name in walk_output[-1]:


with this

counter = 0
inp = 'txt' #normally putting SplitTypes here and getting error 
for drive in drives: # drops every .txt file that 
    for r, d, f in os.walk(drive): #It can get in every disk 
        for file in f:             #(first block) get's every disk's available on system
            filepath = os.path.join(r, file)
            if inp in file: #this line find's every file that ends with .txt
                counter += 1 #this line add's one and goes to the next one
                print(os.path.join(r, file)) #every file' location gets down by down        
print(f"counted {counter} files.") #this line finally gives the count number

Second code block prints out all the file’s extentions such as: txt, png, exe, dll, etc.

['epr',itx', 'itx', 'ilut', 'itx', 'itx', 'cube', 'cube', 'cube', 'itx', 'cube', 'cube''js','dll', 'dll', 'dll', 'json', 'json', 'json', 'json', 'json', 'json', 'json', 'json', 'json', 'json''rar', 'rar', 'ini', 'chm', 'dll', 'dll', 'dll', 'exe', 'sfx', 'sfx', 'exe', 'exe', 'ion', 'txt', 'txt', 'txt', 'exe', 'txt', 'txt', 'txt', 'txt', 
'txt', 'txt', 'txt',]

The problem I’m facing here is I can’t scan for extensions in all drivers (second block of code).
And I can’t search all the files with the extensions that (second block of code) provided to third block of code


The following will print the full filepath to every file on every disk of your Windows system. Note that it may take a very long time to finish.

import os

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

counter = 0
for drive in drives:
    for root, dirs, files in os.walk(drive):
        for file in files:
            print(os.path.join(root, file))
        counter += len(files)

print(f"counted {counter} files.")

Answered By – martineau

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published