Hi all,

I apologise in advance if this is too simple, but I've been searching around for a good hour or two now, and I can't quite get my head around this.

Basically, I'm looking to teach myself Python (3), and one of the things I'm trying to do is put together a simple script that could recursively search an entire drive for a file of a given extension.

I've been looking around, and figured that I'll probably need to use os.walk to recursively search the drive, along with finding other snippets here and there about what I should do. But I can't seem to track down a solid answer, and especially something that'd work in Python 3.

I was wondering if somebody had a few minutes spare, could they put together a simple script that I could look at and use as an example?

Thanks for your time,

Liam.

Recommended Answers

All 5 Replies

This my script in code snippets does it, if text to search is ''.

Thanks for your speedy reply, pyTony, though I feel a bit in over my head reading through that.
From what I can piece together though, does that script also search for text within the actual documents? Because all I need to do is scan the entire drive (C: as it would be in this case) for any file matching the .xyz extension, and create a list of all found files. From there, I'll use that to make a copy of the output to a folder, but that's for a later time.

Sorry if I'm being a bit vague, and thanks again for your time.
Liam.

Yes you can replace the reading the file part and checking for the string (which is kind of half buggy anyway as it reads the file again in memory if there is many strings to check against). You can pass in a list argument instead of find and just append the correct file names in if it matches the spec, I have possiblity for mathching multiple extensions in my script (like .py;.pyw)

Because all I need to do is scan the entire drive (C: as it would be in this case) for any file matching the .xyz extension, and create a list of all found files.

In basic something like this.

import os

file_list = []
for root, dirs, files in os.walk(r'C:\test'):
    for f_name in files:
        if f_name.endswith('.py'):
            file_list.append(f_name)

print(file_list)

If needed also path to files.

import os

file_list = []
for root, dirs, files in os.walk(r'C:\test'):
    for f_name in files:
        file_path = os.path.join(root, f_name)
        if file_path.endswith('.py'):
            file_list.append(file_path)

print(file_list)

endswith() can take a tuple for more than one extension endswith(('.py', '.jpg'))

Thanks for your reply, snippsat, that was really helpful and easy to understand.
I gave the code a shot, and it worked really well. At first, I thought it wasn't searching beyond a depth of one folder, but after placing some dummy files around the drive, I turned out to be wrong.

If I could possibly ask one more question, that'd be fantastic? After getting the list of files (with full path, so using the second block of code), would I be able to straight away use that as a copy list? So for example 'copy file_list to C:\randomdirectory', or is there something else I'd need to do first?

Thanks for your time and help,
Liam.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.