MR_Alex
MR_Alex

Reputation: 11

Problem in list specific files with python

I can list certain files but my code has problems that it only can list files in C:\

I want to change the code so I can scan other drives (for example: drive D, E, F & ...)

How can i do this

Please check my code first and then respond to my question to know what I mean

My code:

import os

def discoverFiles(start):
    extensions = [
        'pdf','mp3','mp4','txt','docx'
    ]

    for dirpath, dirs, files in os.walk(start):
        for i in files:
            absolute_path = os.path.abspath(os.path.join(dirpath, i))
            ext = absolute_path.split('.')[-1]
            if ext in extensions:
                yield absolute_path

x = discoverFiles('/')
for j in x:
    print (j)

Upvotes: 0

Views: 69

Answers (1)

Ha Bom
Ha Bom

Reputation: 2917

You can list file in other drives like this

x = discoverFiles(r'D:\\')

Upvotes: 0

Related Questions