DelPhi1
DelPhi1

Reputation: 45

Finding file in windows path

I'm trying to get someone else's Python script to run on my computer and part of the script finds a file in the USERPROFILE. Here is that code:

    for w in os.walk(os.getenv('USERPROFILE')):
        if 'FilenName' in w[1]: 
            path = str(w[0]) + r'\FilenName\UsrData\Directory\Data'

However, in the above code, the program tries to search in the following directory:

C:\Users\User\AppData\Roaming\

When, in fact, the program should be looking in

C:\Users\User\AppData\Local\

If I replace the problem code with the following, it works, but I need it to run for all USERPROFILEs, not just mine:

path = r'C:\Users\Bill\AppData\Local\FilenName\UsrData\Directory\Data'

What is the solution to this?

Thanks.

Upvotes: 2

Views: 257

Answers (1)

Greg
Greg

Reputation: 1991

I'm not on a windows machine, so this is a bit tricky, but could you find all user profiles using the env var ALLUSERSPROFILE?

Another option may be to replace "Roaming" with "Local" in the string. It's a bit hacky, but can be done:

for w in os.walk(os.getenv('USERPROFILE')):
    if 'FilenName' in w[1]: 
        path = (str(w[0]) + r'\FilenName\UsrData\Directory\Data').replace('Roaming', 'Local')

Upvotes: 1

Related Questions