1

I am using QGIS layer property actions. Is it possible for python to open windows explorer at a directory and then search for a custom string?

Currently I can open windows explorer at the directory I want using:

import subprocess

subprocess.Popen(r'explorer /search, "D:\GIS\Infrastructure"')

But how would I search for a custom string ([%Hospital%] column in attribute file), i.e. open D:\GIS\Infrastructure and list all files that have the word "Hospital" in (as if I was to enter "Hospital" into the search bar of the folder)

closed as off-topic by mgri, Evil Genius, whyzar, John Powell, Mark Ireland Jun 22 '17 at 20:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions relating to general IT or with no clear GIS component, are off-topic here but can be researched/asked at Stack Overflow (software development), Super User (computing hardware and software), Database Administrators (relational databases) and other SE sites" – mgri, Evil Genius, whyzar, John Powell, Mark Ireland
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Unfortunately, this isn't a GIS question, but a question about windows explorer, so it is probably better suited to another site. I found this answer after a quick search. That would probably be a good start. – Evil Genius Jun 22 '17 at 19:58
1

One option you could use is os.walk and .find string method:

import os
searchDirectory = 'C:/Temp'
searchFor = 'Hospital'
for root, dirs, files in os.walk(searchDirectory):
    for f in files:
        found = f.find(searchFor)
        if found > -1:
            print root + '//' + f

Not the answer you're looking for? Browse other questions tagged or ask your own question.