0

I am trying to write code that uses a recursive function to search for files in a directory, and returns the path to the file that matches the search term. However, I keep getting this error when I use "../.." as the path name "PermissionError: [WinError 5] Access is denied: '../..\AppData\Local\Application Data'".

import os
def main():

pathname=input('Please enter path name: ')
filenameinput=input('Please enter file name: ')

def disk_usage(path):

    if os.path.isdir(path):
        for filename in os.listdir(path):
            childpath = os.path.join(path, filename)
            if os.path.isdir(childpath):
                disk_usage(childpath)
            else:
                if childpath.endswith(filenameinput):
                    print(childpath)
#return 
disk_usage(pathname)
main()

I should not need to use os.walk() for this. I have it working but it returns several paths ending in the filename I specified and then returns the WinError 5 thing.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Pongjazzle
  • 67
  • 1
  • 6
  • "I get back errors" - always a classic. Why not tell us what the errors are? – John Zwinck Oct 14 '14 at 23:45
  • 1
    Didn't you ask this question earlier, then delete it when I asked you why you're not just using `os.walk`? – abarnert Oct 14 '14 at 23:47
  • [be nice](http://meta.stackexchange.com/questions/240839/the-new-new-be-nice-policy-code-of-conduct-updated-with-your-feedback) y'all. these counterquestions seem a little meanspirited. – clarkep Oct 15 '14 at 00:02
  • @plarke: What's mean-spirited about asking someone whether they've already asked a question? If this is the same person, there were a number of useful comments on that question. If it's a different person, he has exactly the same question as someone else, whose question got a number of useful comments. Either way, why wouldn't that information be worth knowing? – abarnert Oct 15 '14 at 00:05

1 Answers1

2

You're getting a permission error because Application Data is not a real folder in Windows 7+, it's a "junction" (symlink in Unix-speak) pointing to C:\Program Files. It only exists for backwards compatibility.

You have two options:

  1. You can read the junction with some Windows-specific native code, through win32file. See this SO answer.

  2. You can catch the permission error, and ignore it (maybe print a warning message). This is probably the better option unless you really need to read this folder.

Community
  • 1
  • 1
vgel
  • 3,225
  • 1
  • 21
  • 35