Home > Back-end >  Can't print filepath of all files on all drives
Can't print filepath of all files on all drives

Time:06-01

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]:
        SplitTypes.append(file_name.split(".")[-1])

print(SplitTypes)

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.
Example:

['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 Im building my own antivirus

CodePudding user response:

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

CodePudding user response:

Ok done! Here's how it's done remove the second block no needed
This Will go through every available disks and files
correct me If anything doesn't make sense here I'm still a python newbie

counter = 0
inp = []
for drive in drives:
    for r, d, f in os.walk(drive):
        for file in f:
            filepath = os.path.join(r, file)
            for inp in os.listdir(drive):
                if inp in file:
                    counter  = 1
                    print(os.path.join(r, file))           
print(f"counted {counter} files.")
  • Related